António Pedro
António Pedro

Reputation: 27

Merge two String Arrays using only loops

I'm having some issues with a simple task that I'm not being able to understand. I've got a simple task which is to create an alghoritm that merges two arrays but without repetitions only using for loops...

This is my code so far...

public static String[] mergeReps(String[] a1, String[] a2) {

    // Create a new array with the full size (I'll remove the nulls later)
    String[] retArray = new String[a1.length + a2.length];

    // Copy of array a1 to the retArray
    retArray = Arrays.copyOf(a1, a1.length);

    // Test print...
    System.out.println(Arrays.toString(retArray));

    // loop to check if the indexes value are present in the a1 array
    for (int i = 0; i < a1.length - 1; i++) {
        for (int j = i + 1; j < a2.length; j++) {
            // Condition to check if the value is duplicated
            if (!(a1[j].equalsIgnoreCase(a2[i]))) {
                retArray[i + a1.length] = a2[j];
            }
        }
    }
    return retArray;
}

My question is: How can I compare a2[0] to every single position in the a1 array, and only after doing that and knowing if it's duplicate or not, add it to the retArray?

Upvotes: 0

Views: 146

Answers (3)

Edwin Torres
Edwin Torres

Reputation: 2864

Assuming a1 and a2 each have no duplicates, here is how to merge the arrays without duplicates:

public static String[] mergeReps(String[] a1, String[] a2) {

    //validate the incoming arrays
    if (a1 == null && a2 == null) {
        System.err.println("error - input arrays are null");
        return new String[]{};
    } else if (a1 == null) {
        return a2;
    } else if (a2 == null) {
        return a1;
    }

    int arrEffSize = 0;
    boolean unique;
    String s, sfinal;
    String[] tempArray = new String[a1.length + a2.length];

    //just copy a1 to the tempArray
    System.arraycopy(a1, 0, tempArray, 0, a1.length);
    arrEffSize = a1.length;

    //add String objects from a2, if it isn't already there
    for (int i=0; i < a2.length; i++) {
        unique = true;
        s = a2[i];
        for (int j=0; j < arrEffSize; j++) {
            sfinal = tempArray[j];
            if (s.equalsIgnoreCase(sfinal)) {
                unique = false;
                break;
            }
        }
        if (unique) {
            tempArray[arrEffSize] = s;
            arrEffSize++;
        }
    }   

    //create a new array with the appropriate size, then copy tempArray to it
    String[] retArray = new String[arrEffSize];
    System.arraycopy(tempArray, 0, retArray, 0, retArray.length);

    return retArray;
}

Upvotes: 0

TheCrafter
TheCrafter

Reputation: 1939

You can use this:

for(int nextItemOfSecondArray = a2.length -1; nextItemOfSecondArray >= 0; i--){

    boolean checkIfAlreadyExists = false;

    for(int i = 0; i < a2.length; i++){

        if(a2[nextItemOfSecondArray].equalsIgnoreCase(a1[i])){
            checkIfAlreadyExists = true;
            break;
        }
    }

    if(!checkIfAlreadyExists)
        a1[a1.length] = a2[nextItemOfSecondArray];

}

Upvotes: 0

Mark Carpenter
Mark Carpenter

Reputation: 433

Try this code Snippet. Basically it uses the uniqueness property of a set to compile a set of unique string values. It then converts it to String[].

Check out the javadocs for HashSets for more information on how they work.

  Set<String> result = new HashSet<String>();
  for(String s : a1)
  {
     result.add(s);
  }
  for(String s : a2)
  {
     result.add(s)
  }

  return result.toArray(new String[result.size()]);

Upvotes: 2

Related Questions