-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode349_IntersectionOfTwoArrays.java
More file actions
55 lines (43 loc) · 1.5 KB
/
Copy pathleetcode349_IntersectionOfTwoArrays.java
File metadata and controls
55 lines (43 loc) · 1.5 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class leetcode349_IntersectionOfTwoArrays {
public int[] intersection(int[] nums1, int[] nums2) {
int temp[], temp2[];
HashMap<Integer,Integer> hMap = new HashMap<>();
List<Integer> intersectionList = new ArrayList<>();
if(nums1.length <= nums2.length) {
temp = nums2;
temp2 = nums1;
}
else {
temp = nums1;
temp2 = nums2;
}
for(int i=0;i<temp.length;i++) {
if(hMap.get(temp[i]) == null) {
hMap.put(temp[i],1);
}
else {
hMap.put(temp[i], hMap.get(temp[i])+1);
}
}
for(int j=0;j<temp2.length;j++) {
if(hMap.containsKey(temp2[j]) && hMap.get(temp2[j]) > 0 ){
intersectionList.add(temp2[j]);
hMap.put(temp2[j], 0 );
}
}
//tranfering the items from list to Array
int[] intersectionListArray = new int[intersectionList.size()];
for (int i =0 ; i < intersectionList.size() ; i++) {
intersectionListArray[i] = intersectionList.get(i);
}
return intersectionListArray;
}
public static void main(String[] args) {
int[] nums1 = {1,2,2,1}, nums2 = {2,2};
leetcode349_IntersectionOfTwoArrays obj = new leetcode349_IntersectionOfTwoArrays();
int[] res = obj.intersection(nums1,nums2);
}
}