Practice

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

Inputnums = [1,2,3,1], k = 3Outputtrue

Explanation. indices 0 and 3, distance 3

Example 2

Inputnums = [1,0,1,1], k = 1Outputtrue

Explanation. indices 2 and 3

Example 3

Inputnums = [1,2,3,1,2,3], k = 2Outputfalse

Explanation. 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.