Last Updated: March 21, 2026
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 just asks "are there duplicates anywhere?" This version adds a proximity constraint. That constraint is what makes the problem interesting, because it means we don't need to remember every element we've ever seen. We only need to remember what's in the current window.
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.The most direct approach: for each element, look backward at the previous k elements and check if any of them match. If we find a match, we're done. If we scan the entire array without finding one, we return false.
This is exactly what the problem is asking, translated almost word-for-word into code. For each index i, we check all indices j from max(0, i - k) to i - 1.
i from 0 to n - 1i, iterate backward through indices j from i - 1 down to max(0, i - k)nums[i] == nums[j], return truefalseFor each element, we're scanning up to k previous elements to find a match. What if we stored each value's most recent index so we could check in O(1) whether we've seen it recently enough?
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.
The key insight is that we only need the most recent index. 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 certainly won't be either.
We only need to compare against the most recent occurrence of a value. If the most recent occurrence is too far away, any earlier occurrence will be even farther. By updating the map with the current index every time, we ensure we're always checking the tightest possible distance.
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 is already O(n) time, but the map can grow to hold all n elements even though we only care about elements within distance k. What if we limited our data structure to only hold the k most recent elements?
Here's the idea: 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 just fell out of the window).
This is a classic sliding window pattern. The window represents the "neighborhood" of size k around 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