This is my solution and It takes 80ms but If I use sort which is a nlogn solution It will be much faster

```
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
if (nums.size() <= 1 || k <= 0) return false;
map<int, int> hash;
for (int i = 0; i < nums.size(); i++) {
if (hash.find(nums[i]) == hash.end())
hash[nums[i]] = i;
else if (i-hash[nums[i]] <= k)
return true;
else hash[nums[i]] = i;
}
return false;
}
};
```