-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
27 lines (24 loc) · 841 Bytes
/
Solution.java
File metadata and controls
27 lines (24 loc) · 841 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
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
Map<Integer, Integer> elementMap = new HashMap<Integer, Integer>();
List<Integer> res = new ArrayList<Integer>();
for (int num : nums1) {
if (elementMap.get(num) == null) {
elementMap.put(num, 1);
} else {
elementMap.put(num, elementMap.get(num) + 1);
}
}
for (int num : nums2) {
if (elementMap.get(num) != null && elementMap.get(num) > 0) {
elementMap.put(num, elementMap.get(num) - 1);
res.add(num);
}
}
int[] array = new int[res.size()];
for (int i = 0; i < res.size(); i++) {
array[i] = res.get(i);
}
return array;
}
}