1

Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:

  • answer[0] is a list of all distinct integers in nums1 which are not present in nums2.
  • answer[1] is a list of all distinct integers in nums2 which are not present in nums1.

Note that the integers in the lists may be returned in any order.

I have purposefully left print statement in there for debugging purpose.

class Solution {
    public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        List<List<Integer>> ans = new ArrayList<>();
        for(int i : nums2){
            set2.add(i);
        }
        List<Integer> list = new ArrayList<>();
        for(int i : nums1){
            set1.add(i);
            if(!set2.contains(i)) list.add(i);
        }
        System.out.println(list);
        ans.add(list);
        System.out.println(ans);
        list.clear();
        System.out.println(list);
        for(int i : nums2){
            if(set1.add(i)) {
                list.add(i);
            }
        }
        System.out.println(list);
        ans.add(list);
        System.out.println(ans);

        return ans;
    }
}

input:

nums1 = [1,2,3]
nums2 = [2,4,6]

output:

[1, 3]
[[1, 3]]
[]
[4, 6]
[[4, 6], [4, 6]]

Final value of ans is supposed to be [[1,3],[4,6]] instead of [[4, 6], [4, 6]]. Anyone one know why is it happening?

MarkusAnd
  • 750
  • 7
  • 21
  • 1
    Yes - you've added `list` to `ans` twice. That's two references to the same object. Instead of `list.clear()` I suspect you want `list = new ArrayList<>();` so that you create a *new* list, instead of only clearing the existing one (which is still referenced in `ans`). – Jon Skeet Aug 14 '23 at 13:34
  • You add the same List twice to your `List> ans` instead of adding 2 different List objects. Doing `and.add(list)` will **not** create a copy of `List list`. So any modification you do on that list will be visible everywhere that List is referenced. – OH GOD SPIDERS Aug 14 '23 at 13:35
  • Does this answer your question? [Why does my ArrayList contain N copies of the last item added to the list?](https://stackoverflow.com/questions/19843506/why-does-my-arraylist-contain-n-copies-of-the-last-item-added-to-the-list) – OH GOD SPIDERS Aug 14 '23 at 13:35

1 Answers1

2
ans.add(list);

Here you add reference to the object list, but it's a refference not a copy, so whatever you change in list it's also reflected in ans. If you add System.out.println(ans) after System.out.println(list),

ans.add(list);
System.out.println(ans);
list.clear();
System.out.println(list);
System.out.println(ans);

You'll see [[]]. So, content of the reference is empty. And then at the end of the script you simply add once more refence to list, so simply - whatever you have in list, you have twice in ans. Solution for you is to add copy of the object instead of reference - use:

ans.add(new ArrayList<>(list));

instead of:

ans.add(list);

both times in your code

and if it's fine for you to use standard Java, you can simplify your code like:

public List<List<Integer>> findDifference(Integer[] nums1, Integer[] nums2) {
    Set<Integer> set1 = Set.of(nums1);
    Set<Integer> set2 = Set.of(nums2);
    Set<Integer> set1_not2 = new HashSet<>(set1);
    set1_not2.removeAll(set2);

    Set<Integer> set2_not1 = new HashSet<>(set2);
    set2_not1.removeAll(set1);

    return List.of(new ArrayList<>(set1_not2), new ArrayList<>(set2_not1));
}
Diego Borba
  • 1,282
  • 8
  • 22
Loginus
  • 151
  • 8