# Finding dups

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*.

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

**Brute Force**: We consider each number from 1 to n, and traverse over the whole nums array to check if the current number occurs twice in nums.

**`Honorable mentions: Gauss Formula and Tortoise and Hare technique`**

When encountering problems that involving finding the duplicates of an array of some sort, one of the most useful and elegant techniques for a *O(1) Space Complexity and O(N) time* is to use the numbers in the array as indices to parcour the array and to subsequently set them to negative thereby indicating that they've been visited before hence that number being a duplicate.&#x20;

```python
def findDuplicate(self, nums: List[int]) -> int:
        n = len(nums)
        dup = 0
        
        for x in nums:
            index = abs(x)-1
            if nums[index] > 0:
                nums[index] *= -1
            else:
                dup = abs(x)
        
        return dup
```

Conversely, as seen in the below code, if the identified number is not negative, this signify that it has not been visited before due to the number being missing. Reminder to add 1 as the method to arrive to the element requires the index to be substracted by 1.&#x20;

```python
def findErrorNums(self, nums: List[int]) -> List[int]:
        n = len(nums)
        missing, dup = 0, 0
        
        for x in nums:
            index = abs(x) - 1
            if nums[index] > 0:
                nums[index] *= -1
            else:
                dup = abs(x)
        
        for i in range(n):
            if nums[i] > 0:
                missing = i + 1
        
        return [dup,missing]
```


---

# Agent Instructions: 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://kseb0.gitbook.io/whiteboard/tips/untitled.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.
