> For the complete documentation index, see [llms.txt](https://kseb0.gitbook.io/whiteboard/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kseb0.gitbook.io/whiteboard/tips/untitled.md).

# 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]
```
