Close duplicates
Your task
Given a list of integers and an integer window k, decide whether the list contains two equal values that sit close together: return true if there exist indices i < j with nums[i] == nums[j] and j - i <= k, and false otherwise. For example, [1, 2, 3, 1] with k = 3 is true (the two 1s are 3 apart), but the same list with k = 2 is false. A full pairwise comparison is too slow for large inputs, aim for one pass.
Examples
has_close_duplicate(nums: list[int], k: int) -> boolExample 1
Input
has_close_duplicate([1, 2, 3, 1], 3)Output
TrueExample 2
Input
has_close_duplicate([1, 2, 3, 1], 2)Output
False