← Whiteboard questions

Find the Duplicate Number

Find the repeated number in an array without changing it

DSA, Medium, Linked List

Which structure or algorithm, and what does it cost.

A list nums holds n + 1 whole numbers, and every one is between 1 and n. Exactly one value shows up more than once, possibly many times. Return that value. Leave nums unchanged and use only O(1) extra memory. Aim for O(n) time.

Examples

Input nums = [4, 2, 5, 1, 3, 2]
Output 2
Why n is 5, and 2 is the only value that appears twice.
Input nums = [1, 3, 3, 3, 2, 3]
Output 3
Why the repeated value can appear more than twice.
Input nums = [2, 1, 2]
Output 2
Why the smallest case has n = 2, so the values are 1 or 2, and 2 repeats.

Constraints

Your answer must contain

Target Aim for O(n) time and O(1) extra memory, without changing the list.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

There are n + 1 numbers and only n possible values. What does that tell you about repeats?

Hint 2 the approach

Use each number as an arrow from its position to the position it names. Where must the arrows lead if two positions name the same place?

Hint 3 the key step

Follow the arrows with a slow and a fast pointer until they meet. Then restart one pointer at position 0 and move both one step at a time.

Walkthrough

Full walkthrough brute force to best

Brute force

Compare every pair of positions until two hold the same value. That is O(n^2) time and O(1) memory. With n up to 200,000, it makes billions of comparisons.

Better method

Store the values you have seen in a set and stop at the first one that is already there. The time is O(n), but the set uses O(n) memory.

Sort a copy and look for two equal neighbours. The time is O(n log n), and the copy uses O(n) memory. Sorting nums itself breaks the rule that it must not change.

Best method

Treat position i as a node whose next node is position nums[i]. Every value is between 1 and n, so every arrow lands on a valid position, and position 0 is never the target of an arrow. A repeated value means two positions point to the same node, so the path from 0 must enter a loop, and the entrance of the loop is the repeated value. Find it like a loop in a linked list. Phase one: move slow one arrow and fast two arrows until they meet inside the loop. Phase two: put slow back on 0 and move both one arrow at a time. They meet at the loop entrance, because the distance from 0 to the entrance equals the distance from the meeting point on to the entrance, counted in whole laps. That node is the answer.

Hand trace

Run on [4, 2, 5, 1, 3, 2]. The arrows are 0 to 4, 4 to 3, 3 to 1, 1 to 2, 2 to 5, 5 to 2, so the loop is 2, 5. Phase one: slow and fast start at 0. Step 1: slow 4, fast 3. Step 2: slow 3, fast 2. Step 3: slow 1, fast 2. Step 4: slow 2, fast 2. They meet at 2. Phase two: slow goes to 0, fast stays at 2. Step 1: slow 4, fast 5. Step 2: slow 3, fast 2. Step 3: slow 1, fast 5. Step 4: slow 2, fast 2. They meet at 2, so the answer is 2.

Code

def find_duplicate(nums):
    slow = fast = 0
    while True:                     # phase 1: meet inside the loop
        slow = nums[slow]
        fast = nums[nums[fast]]
        if slow == fast:
            break
    slow = 0
    while slow != fast:             # phase 2: walk to the loop entrance
        slow = nums[slow]
        fast = nums[fast]
    return slow

Time and memory

O(n) time: each phase takes a number of steps linear in n. O(1) extra memory: two pointers. The list is only read.

Common mistakes

  • Sorting nums in place. It is a fast idea, but the question forbids changing the list.
  • Restarting phase two from a different place than phase one. The safest way is to start both phases at position 0, which no arrow points to.
  • Returning the meeting point of phase one. That point is somewhere in the loop, not always the entrance. Run phase two.

Related problems

More Linked List problems
Find the Duplicate Number: walkthrough and Python solution