We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent c4edf2d commit 1daf7c3Copy full SHA for 1daf7c3
EASY/src/easy/IntersectionofTwoArraysII.java
@@ -0,0 +1,37 @@
1
+package easy;
2
+
3
+import java.util.ArrayList;
4
+import java.util.HashMap;
5
+import java.util.List;
6
+import java.util.Map;
7
8
+public class IntersectionofTwoArraysII {
9
10
+ public int[] intersect(int[] nums1, int[] nums2) {
11
+ Map<Integer, Integer> map1 = new HashMap();
12
+ for(int i : nums1){
13
+ if(map1.containsKey(i)){
14
+ map1.put(i, map1.get(i)+1);
15
+ } else {
16
+ map1.put(i, 1);
17
+ }
18
19
20
+ List<Integer> list = new ArrayList();
21
+ for(int i : nums2){
22
+ if(map1.containsKey(i) && map1.get(i) > 0){
23
+ list.add(i);
24
+ map1.put(i, map1.get(i)-1);
25
26
27
28
+ int[] result = new int[list.size()];
29
+ int i = 0;
30
+ for(int num : list){
31
+ result[i++] = num;
32
33
34
+ return result;
35
36
37
+}
0 commit comments