Find the Duplicate Number
Problem Statement
You are given an array nums with n + 1 integers. Every value is between 1 and n. Exactly one value is repeated — it may appear two times or more. Return that value.
You must not modify the array, and you may use only O(1) extra space.
An integer array nums of length n + 1, with 1 ≤ nums[i] ≤ n.
The repeated value.
2 is the only value that appears more than once.
3 appears twice.
The repeated value can appear many times. Here it fills the whole array.
- • 1 ≤ n ≤ 10^5
- • nums.length == n + 1
- • 1 ≤ nums[i] ≤ n
- • Exactly one value repeats (possibly more than twice).
- • Do not modify nums. Use O(1) extra space.
- • Follow-up: can you do it in O(N) time?
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each i, look for an equal value at some j > i
Count elements ≤ mid; more than mid means the duplicate is ≤ mid
Treat nums as a linked list; the start of its cycle is the duplicate
Check every pair (i, j) with i < j. The first equal pair gives the duplicate. It only reads the array and uses no memory, so it follows the rules — it is just slow.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Cyclic Sort translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.