Contains Duplicate II

Given an integer array nums and an integer k, determine whether there are two distinct indices i and j in the array such that:

  • nums[i] == nums[j]
  • abs(i - j) <= k

Return true if such a pair exists. Otherwise, return false.

Example 1
Inputnums = [1,2,3,1], k = 3
Outputtrue
The value 1 appears at indices 0 and 3, and their distance is 3, which is at most k.
Example 2
Inputnums = [1,2,3,1,2,3], k = 2
Outputfalse
Although values repeat, every pair of equal values is more than 2 indices apart.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • 0 <= k <= 10^5

Asked at 13 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate