-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayIntersection.java
More file actions
31 lines (26 loc) · 908 Bytes
/
ArrayIntersection.java
File metadata and controls
31 lines (26 loc) · 908 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
import java.util.HashMap;
public class ArrayIntersection {
public static void printIntersection(int arr1[], int arr2[]) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < arr1.length; i++) {
if (map.containsKey(arr1[i])) {
map.put(arr1[i], map.get(arr1[i]) + 1);
} else {
map.put(arr1[i], 1);
}
}
for (int i = 0; i < arr2.length; i++) {
if (map.containsKey(arr2[i])) {
if (map.get(arr2[i]) > 0) {
System.out.print(arr2[i] + " ");
map.put(arr2[i], map.get(arr2[i]) - 1);
}
}
}
}
public static void main(String[] args) {
int arr1[] = { 1, 4, 5, 2, 2, 3, 6, 5, 3, 2 };
int arr2[] = { 2, 3, 2, 6, 6, 5, 1 };
printIntersection(arr1, arr2);
}
}