Skip to content

Commit f9990fe

Browse files
authored
Create Intersection of Two Arrays II.java
1 parent 2483e02 commit f9990fe

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
public class Solution {
3+
public int[] intersect(int[] nums1, int[] nums2) {
4+
Map<Integer, Integer> map = new HashMap<>();
5+
6+
for (int num : nums1) {
7+
map.put(num, map.getOrDefault(num, 0) + 1);
8+
}
9+
10+
List<Integer> result = new ArrayList<>();
11+
12+
for (int num : nums2) {
13+
if (map.getOrDefault(num, 0) > 0) {
14+
result.add(num);
15+
map.put(num, map.get(num) - 1);
16+
}
17+
}
18+
19+
int[] ans = new int[result.size()];
20+
for (int i = 0; i < result.size(); i++) {
21+
ans[i] = result.get(i);
22+
}
23+
24+
return ans;
25+
}
26+
}

0 commit comments

Comments
 (0)