-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram118.java
More file actions
30 lines (23 loc) · 842 Bytes
/
Program118.java
File metadata and controls
30 lines (23 loc) · 842 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
import java.util.*;
public class Program118 {
// [union problems] | the Intersection of Two Arrays using Hashing in Java
public static void main(String[] args) {
int[] arr1 = {7, 3, 9};
int[] arr2 = {6, 3, 9, 2, 9, 4};
Set<Integer> set = new HashSet<>();
List<Integer> intersection = new ArrayList<>();
// Step 1: Add all elements from arr1 to set
for (int num : arr1) {
set.add(num);
}
// Step 2: Check elements of arr2 for intersection
for (int num : arr2) {
if (set.contains(num)) {
intersection.add(num);
set.remove(num); // Ensure uniqueness
}
}
// Output
System.out.println("Intersection of arrays: " + intersection);
}
}