Skip to content

Instantly share code, notes, and snippets.

@xzhang311
Created April 8, 2016 15:16
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int, int> myMap;
for(int i=0; i<nums.size(); i++){
if(myMap[nums[i]]!=0){
if(abs(i+1-myMap[nums[i]])<=k)
return true;
myMap[nums[i]]=i+1;
}
else
myMap[nums[i]]=i+1;
}
return false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment