25

How can we remove common values from two ArrayLists?

Let’s consider I have two Arraylist as shown below:

ArrayList1 = [1,2,3,4]
ArrayList1 = [2,3,4,6,7]

I would like to have result as:

ArrayListFinal = [1,6,7]

How can I do it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gautam
  • 3,276
  • 4
  • 31
  • 53

4 Answers4

47

Here is an algorithm that you could follow to accomplish the task:

  • Construct a union of the two arrays
  • Construct the intersection of the two arrays
  • Subtract the intersection from the union to get your result

Java collections support addAll, removeAll, and retainAll. Use addAll to construct unions, retainAll for constructing intersections, and removeAll for subtraction, like this:

// Make the two lists
List<Integer> list1 = Arrays.asList(1, 2, 3, 4);
List<Integer> list2 = Arrays.asList(2, 3, 4, 6, 7);
// Prepare a union
List<Integer> union = new ArrayList<Integer>(list1);
union.addAll(list2);
// Prepare an intersection
List<Integer> intersection = new ArrayList<Integer>(list1);
intersection.retainAll(list2);
// Subtract the intersection from the union
union.removeAll(intersection);
// Print the result
for (Integer n : union) {
    System.out.println(n);
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
22

You are actually asking for the Symmetric Difference.

List<Integer> aList = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
List<Integer> bList = new ArrayList<>(Arrays.asList(2, 3, 4, 6, 7));
// Union is all from both lists.
List<Integer> union = new ArrayList(aList);
union.addAll(bList);
// Intersection is only those in both.
List<Integer> intersection = new ArrayList(aList);
intersection.retainAll(bList);
// Symmetric difference is all except those in both.    
List<Integer> symmetricDifference = new ArrayList(union);
symmetricDifference.removeAll(intersection);

System.out.println("aList: " + aList);
System.out.println("bList: " + bList);
System.out.println("union: " + union);
System.out.println("intersection: " + intersection);
System.out.println("**symmetricDifference: " + symmetricDifference+"**");

Prints:

aList: [1, 2, 3, 4]
bList: [2, 3, 4, 6, 7]
union: [1, 2, 3, 4, 2, 3, 4, 6, 7]
intersection: [2, 3, 4]
**symmetricDifference: [1, 6, 7]**
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • @OldCurmudgeon What I have to do if I want an output like [1, 2, 3, 4,6,7] ?? – KK_07k11A0585 Feb 02 '15 at 12:42
  • 1
    @KK_07k11A0585 - That is the `union` of the `symmetric difference` and the `intersection`. – OldCurmudgeon Feb 02 '15 at 12:45
  • I just want the order to be the same. I am getting [2.3,4,1,6,7] or [1,6,7,2,3,4]. The logic I had applied is as folllows (aList - intersection) + (bList - intersection). And It was working [1,2,3,4,6,7]. Thanks :) – KK_07k11A0585 Feb 02 '15 at 13:16
4

You can use something like this:

ArrayList <Integer> first = new ArrayList <Integer> ();
ArrayList <Integer> second = new ArrayList <Integer> ();
ArrayList <Integer> finalResult = new ArrayList <Integer> ();

first.add(1);
first.add(2);
first.add(3);
first.add(4);

second.add(2);
second.add(3);
second.add(4);
second.add(6);
second.add(7);

for (int i = 0; i < first.size(); i++){

    if (!second.contains(first.get(i))){

        finalResult.add(first.get(i));
    }
}


for (int j = 0; j < second.size(); j++){

    if (!first.contains(second.get(j))){

        finalResult.add(second.get(j));
    }

}

I just populated two ArrayLists as you described them in your post, and I checked on both of them for distinct elements; if such element/s was/were found, I add them to the finalResult ArrayList.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Survivor
  • 674
  • 1
  • 7
  • 13
0
SetList<Integer> A = new SetList<Integer>();
A.addAll({1,2,3,4});

SetList<Integer> B = new SetList<Integer>();
B.addAll({2,3,4,6,7});

Integer a = null;

for (int i=0; i<A.size(); i++)
{
    a = A.get(i);

    if (B.contains(a)
    {
        B.remove(a);
        A.remove(a);
        i--;
    }
}

SetList<Integer> final = new SetList<Integer>();
final.addAll(A);
final.addAll(B);

// final = { 1, 6, 7 }
Khaled.K
  • 5,828
  • 1
  • 33
  • 51