How to remove common values from two array lists

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);
}

Leave a Comment