Leetcode 219 Solution

This article provides solution to leetcode question 219 (contains-duplicate-ii)

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;
    }
};