-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersectionOfTwoArrays349.java
More file actions
34 lines (31 loc) · 1.17 KB
/
Copy pathIntersectionOfTwoArrays349.java
File metadata and controls
34 lines (31 loc) · 1.17 KB
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
import java.util.Arrays;
import java.util.HashSet;
/*
Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique, and you may return the result in any order.
*/
public class IntersectionOfTwoArrays349 {
public static void main(String[] args) {
int[] nums1 = {1, 2, 2, 1};
int[] nums2 = {2, 2};
int[] result1 = intersection(nums1, nums2);
for (int num : result1) System.out.print(num + " ");
System.out.println();
int[] nums3 = {4, 9, 5};
int[] nums4 = {9, 4, 9, 8, 4};
int[] result2 = intersection(nums3, nums4);
for (int num : result2) System.out.print(num + " ");
System.out.println();
}
public static int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> hashSet = new HashSet<>();
for (int num1 : nums1) hashSet.add(num1);
int[] result = new int[hashSet.size()];
int index = 0;
for (int num2 : nums2)
if (hashSet.contains(num2)) {
result[index++] = num2;
hashSet.remove(num2);
}
return Arrays.copyOf(result, index);
}
}