-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersectionOfTwoArraysII.java
More file actions
33 lines (30 loc) · 1.05 KB
/
Copy pathIntersectionOfTwoArraysII.java
File metadata and controls
33 lines (30 loc) · 1.05 KB
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
package LeetcodePractice;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class IntersectionOfTwoArraysII {
public static int[] intersect(int[] nums1, int[] nums2) {
Map<Integer, Integer> map = new HashMap<>();
List<Integer> list = new ArrayList<>();
for (int n : nums1) map.put(n, map.getOrDefault(n, 0) + 1);
for (int n : nums2) {
if (map.getOrDefault(n, 0) > 0) {
list.add(n);
map.put(n, map.get(n) - 1);
}
}
int[] result = new int[list.size()];
for (int i = 0; i < list.size(); i++) result[i] = list.get(i);
return result;
}
public static void main(String[] args) {
int[] nums1 = {1, 2, 2, 1};
int[] nums2 = {2, 2};
System.out.println(Arrays.toString(intersect(nums1, nums2)));
int[] nums3 = {4, 9, 5};
int[] nums4 = {9, 4, 9, 8, 4};
System.out.println(Arrays.toString(intersect(nums3, nums4)));
}
}