-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_2215.java
More file actions
29 lines (26 loc) · 1.06 KB
/
Copy pathproblem_2215.java
File metadata and controls
29 lines (26 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
2215. Find the Difference of Two Arrays
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. */
class Solution {
public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
Set<Integer> s1 = new HashSet<>();
Set<Integer> s2 = new HashSet<>();
for(int item : nums1) s1.add(item);
for(int item : nums2) s2.add(item);
List<Integer> onlyin1 = new ArrayList<>();
for(int item : s1){
if(!s2.contains(item)) onlyin1.add(item);
}
List<Integer> onlyin2 = new ArrayList<>();
for(int item : s2){
if(!s1.contains(item)) onlyin2.add(item);
}
List<List<Integer>> result = new ArrayList<>();
result.add(onlyin1);
result.add(onlyin2);
return result;
}
}