-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion17.java
More file actions
32 lines (26 loc) · 892 Bytes
/
question17.java
File metadata and controls
32 lines (26 loc) · 892 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
import java.util.HashSet;
import java.util.ArrayList;
public class question17{
public static void main(String[] args) {
int[] arr1 = {1, 2, 4, 5, 6};
int[] arr2 = {2, 4, 6, 8};
ArrayList<Integer> result = findIntersection(arr1, arr2);
System.out.println("Intersection of arrays:");
for (int num : result) {
System.out.print(num + " ");
}
}
public static ArrayList<Integer> findIntersection(int[] arr1, int[] arr2) {
HashSet<Integer> set1 = new HashSet<>();
HashSet<Integer> intersection = new HashSet<>();
for (int num : arr1) {
set1.add(num);
}
for (int num : arr2) {
if (set1.contains(num)) {
intersection.add(num);
}
}
return new ArrayList<>(intersection);
}
}