From : https://leetcode.com/problems/contains-duplicate-ii/
Given an array of integers and an integer k, find out whether there 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.
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
map<int,int> mp;
int len = nums.size(), num;
for(int i=0; i<len; i++) {
num = nums[i];
if(mp.find(num)==mp.end()) {
mp[num] = i;
} else {
if(i-mp[num]<=k) return true;
else {
mp[num]=i;
}
}
}
return false;
}
};