Dashboard
LC #287

Find the Duplicate Number

Pattern · ?AmazonGoogleMeta

Problem

Find the duplicate in an array of n+1 integers where each integer is in [1, n]. Must not modify the array and use O(1) space.

Constraints

  • 2 ≤ n ≤ 10⁵ (array length n+1)
  • Each integer is in range [1, n]
  • Exactly one duplicate (may appear more than twice)
  • Must not modify the array; O(1) extra space

Example

Input
nums = [1, 3, 4, 2, 2]
Output
2
Why
2 appears twice; all other numbers appear exactly once

Spot the pattern

Which pattern cracks it?

Hints

One at a time
Solve on LeetCode ↗