C++ short solution.


  • 2
    C
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int, int> myMap;
        for (unsigned int i=0; i<nums.size(); i++) {
            if (myMap.find(nums[i]) != myMap.end() && i-myMap[nums[i]]<=k)
                return true;
            myMap[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.