Remove common objects between two List considering the number of occurrences

171 Views Asked by At

Say I have two Java ArrayList and I want to find the difference between two lists, considering the count of each element. For example in above lists, the difference will be an a since list_1 has one extra a:

list_1 = ["a", "a", "b"]
list_2 = ["a", "b"]
diff = ["a"]

I tried to do this by a simple for loop but got concurrent modification exception or invalid result. I also tried to find the common elements using retain and remove them from each list but it fails for example in the following scenario:

List<String> l1 = new ArrayList<>(List.of("a","a"));

List<String> l2 = new ArrayList<>(List.of("a"));

List<String> ret = new ArrayList<>(l1);
ret.retainAll(l2);

// ret will be ["a", "a"] and not ["a"]

sample loop that doesn't work:

List<String> l1 = new ArrayList<>(List.of("b", "a"));

List<String> l2 = new ArrayList<>(List.of("a", "b", "a"));

for (int i = 0; i < l1.size(); i++) {
    if (l2.contains(l1.get(i))) {
        l1.remove(l1.get(i));
        l2.remove(l1.get(i));
    }

// incorrect result: l1: ["a"] , l2: ["a", "b"]
// expected result: l1:[] , l2: ["a"]

Update Updated the loop based on Turing85 comment which seems to work:

for (int i = l1.size() - 1; i >= 0; i--) {
    if (i < l1.size() && l2.contains(l1.get(i))) {
        l2.remove(l1.get(i));
        l1.remove(l1.get(i));
        i++;
    }
}
0

There are 0 best solutions below