Nearby Duplicates
MediumAcc. 91.8%
+30 XP 12
The Proximity Pulse
In high-speed data streams, detecting repeats within a specific "time window" ($K$) is far more important than finding any duplicate at all. By sliding a "window of awareness" across the array, you can instantly detect if a value has reappeared too soon.
The Assignment
Your mission is to detect nearby repeats. Your function receives an array nums and a proximity limit k.
- Use a Set or Object to track the numbers currently inside your "awareness window."
- Iterate through the array:
- If the current number is already in your window, print true and exit.
- Add the current number to your window.
- If the window size exceeds
k, remove the oldest number (nums[i - k]).
- If you reach the end without finding a match, print false.
01EXAMPLE 1
Input
nums=[1,2,3,1], k=3Output
trueExplanation: 1s are 3 steps apart.
Constraints
- Solve in O(n) time using a Sliding Window Hash.
ArraysSliding WindowHashing
JavaScriptSystem handles I/O — write your function only
Loading...
1 Hidden
Input Arguments
nums[1,2,3,1]
k3
Expected Output
true
Click RUN to test your solution