Skip to content

Latest commit

 

History

History
31 lines (24 loc) · 720 Bytes

349 Intersection of Two Arrays 5c2a47dd55e945acab1d28bc7fd8b716.md

File metadata and controls

31 lines (24 loc) · 720 Bytes

349. Intersection of Two Arrays

LeetCode - The World's Leading Online Programming Learning Platform

class Solution {
       public static int[] intersection(int[] nums1, int[] nums2)
    {
        HashSet<Integer> set1 = new HashSet<>();
        HashSet<Integer> set2 = new HashSet<>();

        for (int n : nums1) {
            set1.add(n);
        }

        for (int n : nums2) {
            if (set1.contains(n))
                set2.add(n);
        }

        int [] result = new int[set2.size()];
            int index = 0;
        for (int n : set2){
            result[index++] = n;
        }
        
        return result;
    }

}