We need to determine whether any value appears at least twice within a window of size k. Put differently, for any element at index i, we want to know if the same value appeared at any index j where i - k <= j < i.
The original Contains Duplicate problem asks whether duplicates exist anywhere in the array. This version adds a proximity constraint, and that changes what we need to remember. Instead of tracking every element seen so far, we only need to track the elements in the current window of size k.
1 <= nums.length <= 10^5 → With n up to 100,000, O(n^2) brute force might be too slow in the worst case, but O(n * min(n, k)) could pass depending on k. We should aim for O(n).-10^9 <= nums[i] <= 10^9 → Values range widely, so using values as array indices isn't feasible. We need a hash-based approach.0 <= k <= 10^5 → k can be as large as the array itself, so in the worst case the window is the entire array.Compare each element against the k elements before it. If any of them match, return true. If we scan the entire array without finding a match, return false.
This translates the problem statement directly into code. For each index i, we check every index j from max(0, i - k) up to i - 1.
i from 0 to n - 1i, iterate through indices j from max(0, i - k) up to i - 1nums[i] == nums[j], return truefalseEach element triggers a scan of up to k previous elements. Storing each value's most recent index in a hash map replaces that scan with a single O(1) lookup.
Instead of looking backward from each element, we can flip the perspective. As we scan forward through the array, we keep a hash map that records the most recent index where each value appeared. When we encounter a value that's already in the map, we check whether the stored index is within distance k of the current index.
Only the most recent index matters. If a value appeared at indices 2, 7, and 15, and we're currently at index 17 with k = 3, we only need to check against index 15. If index 15 isn't close enough, indices 2 and 7 are farther away and can't be either. Overwriting the map entry on every occurrence keeps the comparison distance as tight as possible.
inums[i] is already in the map and i - map.get(nums[i]) <= k, return truenums[i] -> i (overwriting any previous index)falseThe hash map approach runs in O(n) time, but the map can grow to hold all n elements even though only the last k matter. The next approach caps the data structure at k elements.
Maintain a hash set that contains exactly the elements in a window of size k. As we move through the array, we add the current element to the set. If it's already there, we found a nearby duplicate. If the set grows beyond size k, we remove the oldest element, the one that fell out of the window.
This is the sliding window pattern. The window holds the k elements before the current index, and the set gives us O(1) membership checks within that window.
inums[i] is already in the set, return true (duplicate within window)nums[i] to the setk, remove nums[i - k] (the element leaving the window)false