Contains Nearby Duplicate

L3 Easy Arrays & Hashing
Concept
Storing "last seen index" inside a dictionary turns a closeness question into constant-time lookups as you sweep the array.
Given nums and a distance k, return true if the same integer appears at two different indices that are at most k apart. Return false otherwise.
Examples
▸ nums = [1, 2, 3, 1], k = 3
→ true
▸ nums = [1, 0, 1, 1], k = 1
→ true
▸ nums = [1, 2, 3, 1, 2, 3], k = 2
→ false
Progressive Hints
Hint 1 · Nudge
Remember where you last saw each value, not just that you saw it.
Hint 2 · Plan
Keep a map from value to its most recent index. For each position: if the value was seen before and the gap between the two indexes is at most k, return true. Always record the current index for that value as you go.
Hint 3 · Approach
last = map from value to index. For each i, n: if n is in last and i - last[n] <= k, return true. Then last[n] = i. Return false.
Output
// Run your code to see the output here.