> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Find the Duplicate Number Python Solution

> Tested Python solution for LeetCode 287 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 287, Medium. Topics: Array, Two Pointers, Binary Search, Bit Manipulation. [View on LeetCode](https://leetcode.com/problems/find-the-duplicate-number/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 287   # by problem number
lcpy gen -s find_the_duplicate_number   # by problem name
```

## Problem

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.

### Examples

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

```
Input: nums = [3,1,3,4,2]
Output: 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?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_the_duplicate_number/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_the_duplicate_number/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def find_duplicate(self, nums: list[int]) -> int:
        """
        Floyd's cycle detection - treat array as implicit linked list.

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

        Array as linked list:
        Index:  0  1  2  3  4
        Value: [1, 3, 4, 2, 2]
                ↓  ↓  ↓  ↓  ↓
        Points: 1  3  4  2  2

        Following pointers: 0→1→3→2→4→2→4→2... (cycle!)

        Visual cycle:
            0
            ↓
            1 ← start
            ↓
            3
            ↓
            2 ←─┐ (duplicate = cycle entrance)
            ↓   │
            4 ──┘

        Phase 1: Find intersection using slow/fast pointers
        Phase 2: Find cycle entrance (duplicate) by resetting slow to start

        The duplicate creates the cycle entrance because multiple indices point to it.
        """
        slow = fast = nums[0]

        # Find intersection point in cycle
        while True:
            slow = nums[slow]
            fast = nums[nums[fast]]
            if slow == fast:
                break

        # Find entrance to cycle (duplicate number)
        slow = nums[0]
        while slow != fast:
            slow = nums[slow]
            fast = nums[fast]

        return slow
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(1)  |

## Tags

[Grind](/catalog/grind), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
