Two for loop Java solution


  • 0
    W
    public int[] intersection(int[] nums1, int[] nums2) {
    	HashSet<Integer> hs1 = new HashSet<>();
    	for (int x : nums1)
    		hs1.add(x);
    
    	int i = 0;
    	for (int x : nums2)
    		if (hs1.contains(x)) {
    			nums1[i++] = x;
    			hs1.remove(x);
    		}
    
    	return Arrays.copyOf(nums1, i);
    }

Log in to reply
 

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