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

Leetcode Problem 442. Find All Duplicates in an Array

442. Find All Duplicates in an Array

Leetcode Solutions

Approach: Mark Visited Elements in the Input Array itself

  1. Initialize an empty list to store the duplicates.
  2. Iterate over each element x in the array.
  3. Calculate the index idx as abs(x) - 1.
  4. If the element at nums[idx] is positive, negate it to mark that the number x has been seen.
  5. If the element at nums[idx] is already negative, it means x has been seen before, so add abs(x) to the list of duplicates.
  6. Return the list of duplicates.
UML Thumbnail

Approach: Store Seen Elements in a Set / Map

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...