bool containsNearbyDuplicate(vector<int>& nums, int k) {
set<int> cand;
for (int i = 0; i < nums.size(); i++) {
if (i > k) cand.erase(nums[ik1]);
if (!cand.insert(nums[i]).second) return true;
}
return false;
}
C++ 6 line solution. simple code and easy understanding

Hi! here is my short C++ solution using hashMap.
bool containsNearbyDuplicate(vector<int>& nums, int k) { unordered_map<int, int> hashMap; for(int i = 0; i < nums.size(); ++i) { if(hashMap.find(nums[i]) != hashMap.end() && i  hashMap[nums[i]] <= k) return true; hashMap[nums[i]] = i; } return false; }

@jcheng.atl
I searched online and found the answer.
The pair::first will store either the newly inserted element or the elements that has its same value in the set.
The pair::second will indicate the success or failure of inserting the new value.
We can use the followings:
if(!set.insert(item).second){
the content of insert failure to user;
}
Thanks

