Java solution using hashSet

  • 1
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set = new HashSet<>();
        Set<Integer> res = new HashSet<>();
        for(int num : nums1) set.add(num);
        for(int num : nums2){
            if(set.contains(num)) res.add(num);
        int[] result = new int[res.size()];
        int i = 0;
        for(int num : res) result[i++] = num; 
        return result;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.