-
Notifications
You must be signed in to change notification settings - Fork 0
/
349.java
26 lines (24 loc) · 890 Bytes
/
349.java
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
// 349. Intersection of Two Arrays
// https://leetcode.com/problems/intersection-of-two-arrays/
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashMap<Integer,Integer> map = new HashMap();
List<Integer> output = new ArrayList();
for(int i = 0; i < nums1.length; i++) {
map.putIfAbsent(nums1[i],0);
map.compute(nums1[i], (k,v)->v+1);
}
for(int i = 0; i < nums2.length; i++) {
Integer val = map.get(nums2[i]);
if (val == null || val.equals(0)){ // if val is not in num1 or already been processed
continue;
}
output.add(nums2[i]);
map.put(nums2[i],0);
}
return output
.stream()
.mapToInt(Integer::intValue)
.toArray();
}
}