-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path125_intersection.java
35 lines (32 loc) · 958 Bytes
/
125_intersection.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
27
28
29
30
31
32
33
34
35
public class Solution {
/**
* @param nums1 an integer array
* @param nums2 an integer array
* @return an integer array
*/
public int[] intersection(int[] nums1, int[] nums2) {
// Write your code here
if(nums1 == null || nums2 == null)
return null;
HashMap<Integer, Integer> map = new HashMap();
for (int i = 0; i < nums1.length; i++) {
if (map.get(nums1[i]) != null){
map.put(nums1[i], map.get(nums1[i]) + 1);
} else {
map.put(nums1[i], 1);
}
}
ArrayList<Integer> list = new ArrayList();
for (int j = 0; j < nums2.length; j++) {
if(map.containsKey(nums2[j]) && map.get(nums2[j]) > 0){
list.add(nums2[j]);
map.put(nums2[j], map.get(nums2[j]) - 1);
}
}
int[] result = new int[list.size()];
for (int k = 0; k < list.size(); k++){
result[k] = list.get(k);
}
return result;
}
}