C++ Solution (32ms)


  • 0
    J
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int, int> hmap;
        for(int i = 0; i<nums.size(); i++) {
            if(hmap.count(nums[i])) {
                if(i - hmap[nums[i]] <= k)
                    return true;
            }
            hmap[nums[i]] = i;
        }
        return false;
    }

Log in to reply
 

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