Contains Duplicate II
Description: Determine whether the array contains two equal elements whose indices differ by at most k.
Example 1:
Input: nums = [1,2,3,1], k = 3
Output: true
Example 2:
Input: nums = [1,2,3,1,2,3], k = 2
Output: false
Constraints:
1 <= nums.length <= 10⁵
0 <= k <= 10⁵
Recommended time and space complexity
Aim for O(n) time.
Hint 1
Checking all pairs is O(n²). What if you remember where a value occurred?
Hint 2
Store the last index of each value in a dictionary.
Hint 3
On a repeat, compare the index difference with k and update the position.

