5 lines of C++ code


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