Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.
Example 1
Input: nums = [1,2,3,1], k = 3
Output: true
Explanation: nums[0] and nums[3] are both 1, and their indices differ by 3, which is within k = 3.
Example 2
Input: nums = [1,0,1,1], k = 1
Output: true
Explanation: nums[2] and nums[3] are both 1 and sit next to each other, a gap of 1 = k.
Example 3
Input: nums = [1,2,3,1,2,3], k = 2
Output: false
Explanation: Every repeated value (1, 2 and 3) appears exactly 3 positions after its first copy. That gap is larger than k = 2, so no pair qualifies.
Remember the most recent index where each value appeared. When you see a value again, check whether the gap to its previous position is within k. Keeping only the latest index is enough because a closer match would always come later.
Run checks all cases above. Submit evaluates all test cases.