bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 219. Contains Duplicate II

219. Contains Duplicate II

Leetcode Solutions

Approach #: Hash Table

  1. Initialize an empty Hash Table.
  2. Iterate over the array using an index variable i.
  3. For each element nums[i], check if it is already in the Hash Table.
    • If it is, return true as we found a duplicate within range k.
  4. Insert the element nums[i] into the Hash Table with the key being the element value and the value being the index i.
  5. If the size of the Hash Table is greater than k, remove the element that is k steps behind the current index i.
  6. If no duplicates are found in the entire array, return false.
UML Thumbnail

Approach #: Naive Linear Search

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR