Simple 6 lines C++


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

Log in to reply
 

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