-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path349. Intersection of Two Arrays
More file actions
36 lines (33 loc) · 959 Bytes
/
Copy path349. Intersection of Two Arrays
File metadata and controls
36 lines (33 loc) · 959 Bytes
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
30
31
32
33
34
35
36
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
Note:
Each element in the result must be unique.
The result can be in any order.
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
HashSet<Integer> set = new HashSet<Integer>();
int l1 = 0, l2 = 0;
while(l1 < nums1.length && l2 < nums2.length) {
if(nums1[l1] < nums2[l2]) {
l1++;
}else if(nums1[l1] > nums2[l2]) {
l2++;
} else {
if(!set.contains(nums1[l1])){
set.add(nums1[l1]);
}
l1++;
l2++;
}
}
int[] res = new int[set.size()];
int i = 0;
for(Integer x : set) {
res[i++] = x;
}
return res;
}
}