1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Solution { public int[] intersection(int[] nums1, int[] nums2) { Set<Integer> set = new HashSet<>(); Set<Integer> ans = new HashSet<>(); for(int i = 0; i < nums2.length; i++){ set.add(nums2[i]); }
for(int i = 0; i < nums1.length; i++){ if(set.contains(nums1[i])){ ans.add(nums1[i]); } } int[] result = new int[ans.size()]; int i = 0 ; for(Integer k : ans){ result[i++] = k; } return result; } }
|