← Home

287. Find the Duplicate Number

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
287

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 287. Find the Duplicate Number, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

The notes already sitting in the source point us in the right direction:

  • Approach #1 Sorting
  • time: O(nlogn), space: O(1) for in-place, O(n) o.w.
  • Approach #2 Set
  • time: O(n), space: O(n)

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are findDuplicate.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n), space: O(1)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Approach #1 Sorting
02//time: O(nlogn), space: O(1) for in-place, O(n) o.w.
03
04//Approach #2 Set
05//time: O(n), space: O(n)
06
07//Approach #3 Floyd's Tortoise and Hare (Cycle Detection)
08//Runtime: 12 ms, faster than 69.11% of C++ online submissions for Find the Duplicate Number.
09//Memory Usage: 9.1 MB, less than 100.00% of C++ online submissions for Find the Duplicate Number.
10//time: O(n), space: O(1)
11class Solution {
12public:
13    int findDuplicate(vector<int>& nums) {
14        int slow = nums[0], fast = nums[0];
15        
16        //phase 1: find the meeting point of Tortoise and Hare
17        //move slow and fast until they meet
18        do{
19            // cout << slow << " " << fast << endl;
20            //move one step
21            slow = nums[slow];
22            //move two steps
23            fast = nums[nums[fast]];
24        }while(slow != fast);
25        // cout << "phase 1 ends: " << endl;
26        // cout << slow << " " << fast << endl;
27        
28        /*
29        phase 2:
30        tortoise starts from nums[0],
31        and now hare's speed is same as tortoise and
32        starts from the meeting point
33        */
34        slow = nums[0];
35        while(slow != fast){
36            // cout << slow << " " << fast << endl;
37            //move one step at a time
38            slow = nums[slow];
39            fast = nums[fast];
40        }
41        // cout << "phase 2 ends: " << endl;
42        // cout << slow << " " << fast << endl;
43        
44        //they will meet at the beginning of the cycle
45        return slow;
46    }
47};

Cost

Complexity

Time
O(n), space: O(1)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.