Given an array of integers and an integer k, return true if and only if there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.
hash map
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int,int> temp;
for(int i=0;i<nums.size();++i){
if(temp.find(nums[i])==temp.end()){
temp[nums[i]] = i;
}
else{
if(k>=(i-temp[nums[i]]))
return true;
}
}
return false;
}
};