Description
Given an integer array nums and an integer k, return true if there are two distinct indices i and j such that nums[i] == nums[j] and abs(i - j) <= k.
Examples
Input:
nums = [1,2,3,1], k = 3Output:
trueExplanation:
1s at indices 0 and 3, distance <= 3.
Input:
nums = [1,0,1,1], k = 1Output:
trueExplanation:
nums[2] and nums[3] are both 1, and |2-3| = 1 <= k.
Input:
nums = [1,2,3,1,2,3], k = 2Output:
falseExplanation:
Duplicate values exist (1,2,3 each appear twice), but the closest duplicate indices are 3 apart, which exceeds k=2.
Constraints
- •
1 ≤ nums.length ≤ 10⁵