Contains Duplicate II - Problem

You are given an integer array nums and a distance threshold k. Your task is to determine if there are any duplicate elements within a specific range.

More specifically, return true if there exist two distinct indices i and j in the array such that:

  • nums[i] == nums[j] (the elements are equal)
  • abs(i - j) <= k (the indices are at most k positions apart)

Otherwise, return false.

Example: In array [1,2,3,1] with k=3, we find that nums[0] = nums[3] = 1 and abs(0-3) = 3 <= 3, so we return true.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1,2,3,1], k = 3
โ€บ Output: true
๐Ÿ’ก Note: The value 1 appears at indices 0 and 3. The distance between these indices is |3-0| = 3, which is exactly equal to k=3, so we return true.
example_2.py โ€” No Valid Duplicates
$ Input: nums = [1,0,1,1], k = 1
โ€บ Output: true
๐Ÿ’ก Note: The value 1 appears at indices 0, 2, and 3. The distance between indices 2 and 3 is |3-2| = 1, which equals k=1, so we return true.
example_3.py โ€” Distance Too Large
$ Input: nums = [1,2,3,1,2,3], k = 2
โ€บ Output: false
๐Ÿ’ก Note: Although there are duplicates (1 appears at indices 0,3 and 2 appears at indices 1,4), all duplicate pairs have distances greater than k=2, so we return false.

Visualization

Tap to expand
๐Ÿ“š Library Book Tracking System๐Ÿ“‹ Checkout RegistryBook: "Algorithm Design"Last Checkout: Day 0Book: "Data Structures"Last Checkout: Day 1Book: "System Design"Last Checkout: Day 2๐Ÿšจ Violation Detected!Someone wants "Algorithm Design"on Day 3Last checkout was Day 0Gap: 3 - 0 = 3 days โ‰ค k=3 โœ“DUPLICATE DETECTED!Algorithm MappingArray Elements โ†’ Book TitlesArray Indices โ†’ Checkout DaysDistance k โ†’ Maximum Days Between CheckoutsHash Map โ†’ Registry of Last Checkout DatesDuplicate Detection โ†’ Same Book Within k Days๐Ÿ“–
Understanding the Visualization
1
Book Registry
Keep a record of when each book was last checked out (hash map)
2
New Checkout
When someone wants to check out a book, look up the last checkout date
3
Time Check
If the same book was checked out within k days, flag it as a violation
4
Update Record
Update the registry with the new checkout date
Key Takeaway
๐ŸŽฏ Key Insight: We only need to remember the most recent occurrence of each element, not all historical positions. This reduces both time and space complexity significantly!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through array, hash map operations are O(1) average case

n
2n
โœ“ Linear Growth
Space Complexity
O(min(n,k))

Hash map stores at most min(n,k) elements since we only need recent indices

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • -109 โ‰ค nums[i] โ‰ค 109
  • 0 โ‰ค k โ‰ค 105
  • Note: Two distinct indices means i โ‰  j
Asked in
Amazon 45 Google 38 Microsoft 32 Apple 28 Meta 25
58.5K Views
High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen