Learn
Practice
Newsletter
Resources
Mobile
New
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Contains Duplicate II
Bookmark
Input
Within k
Too far
Adjacent
All distinct
Custom
nums
=
[1, 2, 3, 1]
,
k
=
3
1
2
3
1
0
1
2
3
window
(empty)
slide a window of the last k values across the array
1
2
3
1
0
1
2
3
window
(empty)
slide a window of the last k values across the array
1
2
3
1
0
1
2
3
window
(empty)
window =
{ }
the last k values
window ≤ k = 3
i
1
2
3
1
0
1
2
3
window
(empty)
nums[0] =
1
in the window?
window ≤ k = 3
i
1
2
3
1
0
1
2
3
window
1
window.add(
1
)
window ≤ k = 3
i
1
2
3
1
0
1
2
3
window
1
nums[1] =
2
in the window?
window ≤ k = 3
i
1
2
3
1
0
1
2
3
window
1
2
nums[2] =
3
in the window?
window ≤ k = 3
i
1
2
3
1
0
1
2
3
window
1
2
3
window.add(
3
)
window ≤ k = 3
i
1
2
3
1
0
1
2
3
window
1
2
3
nums[3] =
1
in the window?
window ≤ k = 3
dist 3 ≤ 3
1
2
3
1
0
1
2
3
window
1
2
3
1
∈ window
nearby duplicate!
dist 3 ≤ 3
1
2
3
1
0
1
2
3
window
1
2
3
return
true
algo
master
.
io
Step:
Is there a repeat within distance k = 3? Keep a window of the last k values
0 / 10
Input
Within k
Too far
Adjacent
All distinct
Custom
nums
=
[1, 2, 3, 1]
,
k
=
3
0 / 10
algo
master
.
io
Step:
Is there a repeat within distance k = 3? Keep a window of the last k values