LeetCode – Contains Duplicate II (Java)

Given an array of integers and an integer k, return true if and only if there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.

Java Solution 1 – HashMap

public boolean containsNearbyDuplicate(int[] nums, int k) {
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
 
    for(int i=0; i<nums.length; i++){
        if(map.containsKey(nums[i])){
            int pre = map.get(nums[i]);
            if(i-pre<=k)
                return true;
        }
 
        map.put(nums[i], i);
    }
 
    return false;
}

Java Solution 2 – HashSet

public boolean containsNearbyDuplicate(int[] nums, int k) {
    if(nums==null || nums.length<2 || k==0)
        return false;
 
    int i=0; 
 
    HashSet<Integer> set = new HashSet<Integer>();
 
    for(int j=0; j<nums.length; j++){
        if(!set.add(nums[j])){
            return true;
        }            
 
        if(set.size()>=k+1){
            set.remove(nums[i++]);
        }
    }
 
    return false;
}

7 thoughts on “LeetCode – Contains Duplicate II (Java)”

  1. A bit simpler solution – just one pointer `i`:

    boolean containsDuplicates(int[] nums, int k)
    {
    if (nums == null || nums.length <= 1 || k <= 0) return false;

    HashSet exists = new HashSet(k); // TODO: take into control load factor.

    for (int i = 0 ; i k)
    {
    exists.remove(nums[i-k]);
    }
    }

    return false;
    }

  2. Without the HashSet, you’ll traverse k times for each element, in order to compare to the previous k, meaning O(k*n).
    With the HashSet, it’s O(n).

  3. You are right. My code for this solution:

    public boolean containsNearbyDuplicate(int[] nums, int k) {
    HashSet set = new HashSet();

    for (int i = 0; i = k)
    set.remove(nums[i - k]);
    }

    return false;
    }

  4. Is there a way to do this O(n) time and O(1) space though? I’m just thinking how would we go about proving that there isn’t?

Leave a Comment