-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayIntersection.java
More file actions
39 lines (28 loc) · 895 Bytes
/
Copy pathArrayIntersection.java
File metadata and controls
39 lines (28 loc) · 895 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
package LeetcodePractice;
import java.util.*;
public class ArrayIntersection {
public static int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<>();
Set<Integer> resultSet = new HashSet<>();
for (int num : nums1) {
set1.add(num);
}
for (int num : nums2) {
if (set1.contains(num)) {
resultSet.add(num);
}
}
int[] result = new int[resultSet.size()];
int i = 0;
for (int num : resultSet) {
result[i++] = num;
}
return result;
}
public static void main(String[] args) {
int[] nums1 = {4, 9, 5};
int[] nums2 = {9, 4, 9, 8, 4};
int[] result = intersection(nums1, nums2);
System.out.println("Intersection: " + Arrays.toString(result));
}
}