PIXELBANKv8.2.1
Menu

Contains Duplicate II

Given an array and integer k, return True if there are two distinct indices i and j such that nums[i] == nums[j] and abs(i - j) <= k.

Example:

Input:
1,2,3,1
3
Output:
True
Reasoning:
  • The input array is [1, 2, 3, 1] and the integer k is 33.
  • We iterate through the array to find duplicate elements within a distance of kk indices.
  • At index 00, the value is 11, and at index 33, the value is also 11, with an index difference of abs(03)=3abs(0-3) = 3, which satisfies the condition abs(ij)kabs(i - j) \leq k.
  • Since a duplicate is found within the specified distance, the function returns True.

Constraints:

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • 0 <= k <= 10^5
Editor

Test Results

0/0
Run code to see test results.