-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintIntersection.java
More file actions
40 lines (34 loc) · 916 Bytes
/
PrintIntersection.java
File metadata and controls
40 lines (34 loc) · 916 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.*;
public class Solution {
public static void intersection(int[] arr1, int[] arr2) {
Arrays.sort(arr1);
Arrays.sort(arr2);
HashMap<Integer,Integer> map = new HashMap<>();
for(int i =0;i<arr1.length;i++)
{
if(map.containsKey(arr1[i]))
{
int value = map.get(arr1[i]);
value+=1;
map.put(arr1[i],value);
}
else
{
map.put(arr1[i],1);
}
}
for(int j = 0;j<arr2.length;j++)
{
if(map.containsKey(arr2[j]))
{
int value = map.get(arr2[j]);
if(value>0)
{
System.out.print(arr2[j]+" ");
value--;
map.put(arr2[j],value);
}
}
}
}
}