Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

Find the Duplicate Number

Medium

Topics
ArrayTwo PointersBinary Search

Given an array nums of n+1 integers where each value is in [1, n], exactly one value is repeated. Return the repeated number without modifying the array and using O(1) extra space.

Example 1

Input:  nums = [1,3,4,2,2]
Output: 2

Example 2

Input:  nums = [3,1,3,4,2]
Output: 3

Constraints

  • 1 <= n <= 10^5
  • nums.length == n + 1
  • Only one repeated number, but it may appear more than once.
Run ⌘' · Submit ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access