Contains Duplicate II
Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.

30:00

Contains Duplicate II
easy
Topics
Companies

Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.

Example 1:
Input: {"nums":[1,2,3,1],"k":3}
Output: true
Constraints:
  • 1nums.length1051 \leq \text{nums.length} \leq 10^5

  • 109nums[i]109-10^9 \leq \text{nums}[i] \leq 10^9

  • 0k1050 \leq k \leq 10^5

Input
arr ={"nums":[1,2,3,1],"k":3}

Check if duplicates exist within distance k=3

Window Size (k)

3

Array (sliding window of size k)
1
0
2
1
3
2
1
3
Seen Map (value → last index)

Empty

Current
In Window
Duplicate Found
Variables
No variables to display
DepthFunction Call
Stack empty
0/8