JAVA solution using sort


  • 1
    Y
    public int[] intersection(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int i= 0;
        int j = 0;
        HashSet<Integer> set = new HashSet<Integer>();
        while(i < nums1.length && j < nums2.length){
            if(nums1[i] == nums2[j]){
                set.add(nums1[i]);
                i++;
                j++;
            }
            else if(nums1[i] < nums2[j]){
                i++;
            }
            else{
                j++;
            }
        }
        int index = 0;
        int[] rst = new int[set.size()];
        for(int k : set){
            rst[index++] = k;
        }
        return rst;
    }

Log in to reply
 

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