Leetcode 219 Solution
This article provides solution to leetcode question 219 (contains-duplicate-ii)
Access this page by simply typing in "lcs 219" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/contains-duplicate-ii
Solution
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_set<int> a;
for (int i = 0; i < nums.size(); i++)
{
auto v = nums[i];
if (a.find(v) != a.end())
return true;
a.insert(v);
if (i >= k)
a.erase(nums[i - k]);
}
return false;
}
};