Contains Duplicate II
Module 6 · Hash Tables
Problem
Given nums and an integer k, return whether there exist two equal
elements whose indices differ by at most k.
Examples
Example 1
Input
nums = [1,2,3,1], k = 3OutputtrueExplanation. indices 0 and 3, distance 3
Example 2
Input
nums = [1,0,1,1], k = 1OutputtrueExplanation. indices 2 and 3
Example 3
Input
nums = [1,2,3,1,2,3], k = 2OutputfalseExplanation. equal values are 3 apart
Constraints
1 ≤ n ≤ 10⁵ · 0 ≤ k ≤ 10⁵.
Attempt it first
Plain Contains Duplicate is the Seen verb from the patterns lesson. The distance condition is the new ingredient — there are two clean ways to absorb it, one per hint. Find at least one.