Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Contains Duplicate II
Bookmark
Brute Force
Hash Map
Sliding Window
Input
Within k
Too far
Adjacent
All distinct
Custom
nums
=
[1, 2, 3, 1]
,
k
=
3
1
2
3
1
0
1
2
3
test every pair that could be close enough to count
1
2
3
1
0
1
2
3
test every pair that could be close enough to count
k =
3
comparisons
0
1
2
3
1
0
1
2
3
i
i = 0, value
1
nothing is within k of it
k =
3
comparisons
0
j ≥ i - k = 0
1
2
3
1
0
1
2
3
i
i = 1, value
2
check j = 0 to 0
k =
3
comparisons
1
j ≥ i - k = 0
1
2
3
1
0
1
2
3
i
j
2
≠
1
nums[1] vs nums[0]
k =
3
comparisons
1
j ≥ i - k = 0
1
2
3
1
0
1
2
3
i
i = 2, value
3
check j = 0 to 1
k =
3
comparisons
2
j ≥ i - k = 0
1
2
3
1
0
1
2
3
i
j
3
≠
1
nums[2] vs nums[0]
k =
3
comparisons
3
j ≥ i - k = 0
1
2
3
1
0
1
2
3
i
j
3
≠
2
nums[2] vs nums[1]
k =
3
comparisons
3
j ≥ i - k = 0
1
2
3
1
0
1
2
3
i
i = 3, value
1
check j = 0 to 2
k =
3
comparisons
4
j ≥ i - k = 0
dist 3 ≤ 3
1
2
3
1
0
1
2
3
i
j
1
==
1
3 - 0 = 3 ≤ 3
k =
3
comparisons
4
dist 3 ≤ 3
1
2
3
1
0
1
2
3
return
true
algo
master
.
io
Step:
Check every pair no more than k = 3 apart, and stop at the first repeat
0 / 9
Input
Within k
Too far
Adjacent
All distinct
Custom
nums
=
[1, 2, 3, 1]
,
k
=
3
0 / 9
algo
master
.
io
Step:
Check every pair no more than k = 3 apart, and stop at the first repeat