ExamAdda Logo

Find the Duplicate Number

Easy

Given an array nums containing n + 1 integers where:

  • Each integer is in the range [1, n]
  • There is only one repeated number, but it may appear more than once

Return the number that appears more than once.

You must solve the problem without modifying the array and using only constant extra space.

Example 1

Input

nums = [1,3,4,2,2]

Output

2

Explanation

The number 2 appears more than once.

Example 2

Input

nums = [3,1,3,4,2]

Output

3

Explanation

The number 3 appears twice, so the duplicate number is 3.

Constraints

1 <= n <= 10^5
nums.length == n + 1
1 <= nums[i] <= n
All the integers in nums appear exactly once except for one integer
which appears two or more times.

Hints:

Hint 1

Think of every array value as pointing to another index.

Hint 2

You can treat the array like a linked list and use Floyd's Cycle Detection Algorithm.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 10, 2026

Last updated

Aug 11, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output