287. Find the Duplicate Number

Find the Duplicate Number (LeetCode 287) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Two Pointers pattern, in the Fast & Slow (Cycle Detection) subpattern.

How to solve Find the Duplicate Number

Find the Duplicate Number is a Floyd's Cycle Detection problem, part of Two Pointers. Master Floyd's tortoise and hare algorithm for cycle detection. Detect cycles in linked lists and find duplicate numbers.

Work towards the solution in these steps:

  1. Think about how the problem can be represented as a linked structure where elements point to the index of the next element, enabling cycle detection techniques.
  2. Recall that the presence of a duplicate number implies a cycle in this representation, so cycle detection algorithms like Floyd's Tortoise and Hare can be applied.
  3. Use two pointers moving at different speeds to detect a cycle in the sequence formed by following the indices in the array.

The reference solution runs in O(n) time and O(1) space.

The Find the Duplicate Number solution and editorial walks through the full approach with reference code.

Problem statement

Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.

There is only one repeated number in nums, return this repeated number.

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

Example 1:

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

Example 2:

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

Example 3:

Input: nums = [3,3,3,3,3] Output: 3

Constraints:

- 1 <= n <= 10^5

- nums.length == n + 1

- 1 <= nums[i] <= n

- All the integers in nums appear only once except for precisely one integer which appears two or more times.

Follow up:

- How can we prove that at least one duplicate number must exist in nums?

- Can you solve the problem in linear runtime complexity?

Topics and companies

Topics: Array, Two Pointers, Binary Search, Bit Manipulation.

Reported in interviews at Microsoft, Amazon, Apple, Google, Bloomberg, Paypal, Goldman Sachs.

How to practise Find the Duplicate Number on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.

Open Find the Duplicate Number in the code editor, or read the Find the Duplicate Number editorial for a worked solution with its approach and complexity analysis.

Where Find the Duplicate Number sits in the DSA pattern sheet

Problems related to Find the Duplicate Number

Other problems that use the same Fast & Slow (Cycle Detection) and Two Pointers techniques:

Find the Duplicate Number FAQ

What is the LeetCode number of Find the Duplicate Number?

Find the Duplicate Number is LeetCode problem 287, rated Medium.

What pattern is used to solve Find the Duplicate Number?

Find the Duplicate Number is a Floyd's Cycle Detection problem. It belongs to the Two Pointers pattern, in the Fast & Slow (Cycle Detection) subpattern.

What is the time complexity of Find the Duplicate Number?

The reference solution runs in O(n) time and O(1) space.

Which companies ask Find the Duplicate Number?

Find the Duplicate Number has been reported in interviews at Microsoft, Amazon, Apple, Google, Bloomberg, Paypal, Goldman Sachs.

Preparing this workspace.