dsa · easy
Contains Duplicate II
Return whether nums contains two equal values whose indices differ by at most k.
Arguments
nums— array of integersk— maximum allowed index distance
Example
nums = [1,2,3,1], k = 3 → true (the two 1s are 3 apart).
nums = [1,2,3,1,2,3], k = 2 → false.
Constraints
1 <= nums.length <= 10^5 0 <= k <= 10^5 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [1,2,3,1] 3 Expected: true
Example 2
Input: [1,0,1,1] 1 Expected: true
Example 3
Input: [1,2,3,1,2,3] 2 Expected: false