Skip to content

Commit 1a0dd0d

Browse files
authored
Create Solution.java
1 parent 387eb21 commit 1a0dd0d

File tree

1 file changed

+22
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)