Faaez Razeen

Find the Duplicate Number

  • 2 min read
  • LC-Medium
  • Linked List

3 years ago

Solution

TimeSpaceExplanation
O(n)O(1)
def findDuplicate(self, nums: List[int]) -> int: # Find intersection point between slow and fast slow = fast = 0 while True: slow = nums[slow] fast = nums[nums[fast]] if slow == fast: break # Create new pointer slow2 that starts from the beginning # Advance slow and slow2 at same speed- the point at which they meet is the duplicate number slow2 = 0 while slow != slow2: slow = nums[slow] slow2 = nums[slow2] return slow