219.Contains Duplicate II
Example 1:
Input: nums = [1,2,3,1], k = 3
Output: true
Example 2:
Input: nums = [1,0,1,1], k = 1
Output: true
Example 3:
Input: nums = [1,2,3,1,2,3], k = 2
Output: falseclass Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int, vector<int >> cnt;
for(int i=0;i<nums.size();i++)
cnt[nums[i]].push_back(i);
for(auto c:cnt)
if(c.second.size()>1)
{
for(int i=1;i<c.second.size();i++)
if(c.second[i]-c.second[i-1]<=k)
return true;
}
return false;
}
};Last updated