Contains Duplicate II
Given an array and integer k, return True if there are two distinct indices i and j such that nums[i] == nums[j] and abs(i - j) <= k.
Example
1,2,3,1 3
True
- The inputs are the list
nums= (positions to ) and the window sizek= , the largest gap between two positions that still counts as "nearby". - A qualifying pair needs two different positions holding the same value, with . Equal values far apart do not count.
- In the sample, the values and each appear once, so they can never form a pair. The only repeated value is , at positions and .
- Their gap is , and , so the condition holds and the answer is
True. - Notice how tight this is: with the same list gives
False, because the two s sit exactly apart. Also, can never succeed, since the two positions must be distinct.
Constraints
- 1 <= len(nums) <= 10^5
- -10^9 <= nums[i] <= 10^9
- 0 <= k <= 10^5