13 · Sliding Window

#219 Contains Duplicate II

Easysolve on LeetCode ↗

frequently reported atNetflixAirbnbAmazonGoogleMicrosoft

the problem

In plain words

Return true if the array holds the same value at two different indices i and j that are at most k apart (|i − j| ≤ k). Otherwise return false.

in nums = [1,2,3,1], k = 3

out true

The two 1s are 3 apart.

in nums = [1,0,1,1], k = 1

out true

Indices 2 and 3.

in nums = [1,2,3,1,2,3], k = 2

out false

Every repeat is 3 apart.

  • 1 ≤ n ≤ 10⁵
  • -10⁹ ≤ nums[i] ≤ 10⁹
  • 0 ≤ k ≤ 10⁵

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #217
      Contains Duplicate
      Easyin Hashing
    2. #220
    3. #3