My JS solution 115ms


  • 4
    M
    var containsNearbyDuplicate = function(nums, k) {
        if(nums.length <= 1 || k < 1)
        {
            return false;
        }
        var map = {};
        for(var i=0; i<nums.length; i++)
        {
            if(map[nums[i]] !== undefined)
            {
                return true;
            }
            else 
            {
                if(i - k >=0)
                {
                    map[nums[i-k]] = undefined;
                }
                map[nums[i]] = true;
            }
        }
        return false;
    };

Log in to reply
 

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