Contains Duplicate II
Given an integer array nums and an integer k, determine whether there are two distinct indices i and j in the array such that:
nums[i] == nums[j]abs(i - j) <= k
Return true if such a pair exists. Otherwise, return false.
Example 1
Input
nums = [1,2,3,1], k = 3Output
trueThe value 1 appears at indices 0 and 3, and their distance is 3, which is at most k.
Example 2
Input
nums = [1,2,3,1,2,3], k = 2Output
falseAlthough values repeat, every pair of equal values is more than 2 indices apart.
Constraints
- 1 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
- 0 <= k <= 10^5