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 iand j is at most k.
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int, int> m_map;
for(int i = 0; i < nums.size(); i++)
{
if(m_map.find(nums[i]) == m_map.end())
{
m_map[nums[i]] = i;
}
else
{
if(std::abs(m_map[nums[i]] - i) <= k)
return true;
else
m_map[nums[i]] = i;
}
}
return false;
}
};