-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersectionHashMap.java
More file actions
40 lines (31 loc) · 889 Bytes
/
IntersectionHashMap.java
File metadata and controls
40 lines (31 loc) · 889 Bytes
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
import java.util.HashMap;
public class Intersection{
public static void intersection(int[] arr1, int[] arr2){
HashMap<Integer,Integer> map=new HashMap();
int n=arr1.length;
int m=arr2.length;
for(int i=0;i<n;i++)
{
if(map.containsKey(arr1[i]))
{
int freq=map.get(arr1[i]);
map.put(arr1[i],freq+1);
}
else
map.put(arr1[i],1);
}
for(int i=0;i<m;i++)
{
if(map.containsKey(arr2[i]))
{
int f=map.get(arr2[i]);
System.out.println(arr2[i]);
map.put(arr2[i],f-1);
if(f-1==0)
{
map.remove(arr2[i]);
}
}
}
}
}