1import java.util.HashSet;
2import java.util.Set;
3
4
25public class Intersection {
26
33 public static int[] intersection(int[] nums1, int[] nums2) {
34 Set<Integer> set = new HashSet<>();
35 Set<Integer> res = new HashSet<>();
36
37
38 for (int i = 0; i < nums1.length; i++) {
39 set.add(nums1[i]);
40 }
41 for (int i = 0; i < nums2.length; i++) {
42 if (set.contains(nums2[i])) {
43 res.add(nums2[i]);
44 }
45 }
46
47
48 int resArray[] = new int[res.size()];
49 int j = 0;
50 for (Integer r : res) {
51 resArray[j++] = r.intValue();
52 }
53
54 return resArray;
55 }
56
57 public static void main(String[] args) {
58 int [] nums1 = {4,9,5}, nums2 = {9,4,9,8,4};
59 intersection(nums1,nums2);
60 }
61}