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

Leetcode Problem 287. Find the Duplicate Number

287. Find the Duplicate Number

Leetcode Solutions

Floyd's Tortoise and Hare (Cycle Detection)

  1. Initialize two pointers tortoise and hare to the first element of the array.
  2. Move tortoise to nums[tortoise] and hare to nums[nums[hare]] until they meet.
  3. Once they meet, reinitialize tortoise to the first element.
  4. Move both tortoise and hare one step at a time (tortoise to nums[tortoise] and hare to nums[hare]) until they meet again.
  5. The meeting point is the duplicate number, return it.
UML Thumbnail

Binary Search

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...