-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathIntersectionOfTwoArraysII_350.java
66 lines (49 loc) · 1.4 KB
/
IntersectionOfTwoArraysII_350.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package easy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class IntersectionOfTwoArraysII_350 {
public static int[] intersect(int[] nums1, int[] nums2) {
HashMap<Integer, Integer> map = new HashMap<>();
ArrayList<Integer> intersection = new ArrayList<>();
// store all items of nums1 in hashmap with their count
for (int item1 : nums1) {
if (!map.containsKey(item1)) {
map.put(item1, 1);
} else {
map.put(item1, map.get(item1) + 1);
}
}
for (int val : nums2) {
if (map.containsKey(val)) {
int currentCount = map.get(val);
if (currentCount >= 1) {
intersection.add(val);
// for intersection item that occur more than 1 time
map.put(val, currentCount - 1);
}
// there can be duplicate elements
if (currentCount == 0) {
map.remove(val);
}
}
}
// to return final result, we need array
int[] ans = new int[intersection.size()];
// copying intersection result in array from ArrayList
for (int i = 0; i < ans.length; i++) {
ans[i] = intersection.get(i);
}
return ans;
}
public static void main(String[] args) {
// test case 1
// int[] nums1 = { 1, 2, 2, 1 };
// int[] nums2 = { 2, 2 };
// test case 2
int[] nums1 = { 4, 9, 5 };
int[] nums2 = { 9, 4, 9, 8, 4 };
int[] intersection = intersect(nums1, nums2);
System.out.println(Arrays.toString(intersection));
}
}