O(n) c++ solution


  • 0
    A

    bool containsNearbyDuplicate(vector<int>& nums, int k) {
    if(k==0) return false;
    unordered_map<int,int> track;
    for(int i=0;i<nums.size();i++) {
    if(track.find(nums[i])!=track.end()) return true;
    if(i>=k) {
    track.erase(nums[i-k]);
    }
    track.insert({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.