Nearby Duplicate
Return True if the list nums has two different positions i and j with nums[i] == nums[j] and abs(i - j) <= k (k is the second argument). Otherwise return False.
Example 1
nums [1,2,3,1]
k 3
returns true
The 1s are at positions 0 and 3, distance 3, which is allowed.
Example 2
nums [1,2,3,1]
k 2
returns false
The 1s are 3 apart, more than 2.
Constraints
1 <= len(nums) <= 500 <= k <= 60
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.