> For the complete documentation index, see [llms.txt](https://nataliekung.gitbook.io/ladder_code/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nataliekung.gitbook.io/ladder_code/qiang-hua-4-shuang-zhi-zhen-ff09/find-the-duplicate-number.md).

# Find the Duplicate Number

Given an array nums containing n+ 1 integers where each integer is between 1 and n(inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

**Example 1:**

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

**Example 2:**

```
Input:
 [3,1,3,4,2]

Output:
 3
```

**Note:**

```
Note:

You must not modify the array (assume the array is read only).
You must use only constant, O(1) extra space.
Your runtime complexity should be less than O(n2).
There is only one duplicate number in the array, but it could be repeated 
more than once.
```

二分法

```
At first the search space is numbers between 1 to n. 
Each time I select a number mid (which is the one in the middle) and 
count all the numbers equal to or less than mid. Then if the count is 
more than mid, the search space will be [1 mid] otherwise [mid+1 n]. 
I do this until search space is only one number.
```

```
class Solution:
    def findDuplicate(self, nums: List[int]) -> int:
        l = len(nums)
        start, end = 1, l-1
        while start < end:
            mid = start + (end - start)//2
            cnt = 0
            for i in nums:
                if i <= mid:
                    cnt += 1
            if cnt > mid:
                end = mid
            else:
                start = mid + 1
        return start
```

快慢指针

```
For this example, [2 5 1 1 4 3]

Let's point all elements to their corresponding indice. 
2 points to index 1 which is number 5. 
5 points to index 4 which is number 4. 
1 points to index 0 which is number 2. 
1 points to index 0 which is number 2. 
4 points to index 3 which is number 1. 
3 points to index 2 which is number 1.
Draw this on a paper and you will find 2 -> 5 -> 4 -> 1 -> 2 which shapes a 
circle.
Hopes this helps.
```

```
class Solution:
    def findDuplicate(self, nums: List[int]) -> int:
        l = len(nums)
        slow,quick=nums[0],nums[nums[0]]
        while slow!= quick:
            slow = nums[slow]
            quick = nums[nums[quick]]

        slow = 0
        while slow!=quick:
            slow = nums[slow]
            quick = nums[quick]
        return slow
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nataliekung.gitbook.io/ladder_code/qiang-hua-4-shuang-zhi-zhen-ff09/find-the-duplicate-number.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
