Short HashSet Solution


  • 21
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        HashSet<Integer> hs=new HashSet<>();
        for(int i=0;i<nums.length;i++)
        {
            if(hs.add(nums[i])==false) return true;
            if(hs.size()==k+1) hs.remove(nums[i-k]);
        }
        return false;
    }

  • 0
    This post is deleted!

Log in to reply
 

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