https://leetcode.com/problems/contains-duplicate-ii/

Source


    bool containsNearbyDuplicate(const vector<int>& nums, int k) {
        unordered_map<int, int> hash;
        for (int i=0; i<nums.size(); ++i) {
            if (++hash[nums[i]] > 1) return true;
            if (i>=k) hash[nums[i-k]] = 0;
        }
        return false;
    }

GitHub

ContainsDuplicate2