JAVA 6ms Solution


  • 1
    1

    public class Solution {

    public int[] intersection(int[] nums1, int[] nums2) {
        int len1 = nums1.length;
        int len2 = nums2.length;
        int[] res = new int[len2];    
        Arrays.sort(nums2);
        Set<Integer> set1 = new HashSet<Integer>();
        
        for(int i=0; i<len1; i++){
            set1.add(nums1[i]);
        }
        
        int index = 0;
        for(int i=0; i<len2; i++){
            if(set1.contains(nums2[i])){
                if(index==0){ 
                    res[index++] = nums2[i];
                }else if(nums2[i]!=res[index-1]){
                    res[index++] = nums2[i];
                }
            }  
        }
        return Arrays.copyOfRange(res, 0, index);
    }
    

    }


Log in to reply
 

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