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

# N-Repeated Element in Size 2N Array

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

LeetCode 961, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table). [View on LeetCode](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/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 961   # by problem number
lcpy gen -s n_repeated_element_in_size_2n_array   # by problem name
```

## Problem

You are given an integer array `nums` with the following properties:

* `nums.length == 2 * n`.
* `nums` contains `n + 1` **unique** values, `n` of which occur **exactly once** in the array.
* Exactly one element of `nums` is repeated `n` times.

Return the element that is repeated `n` times.

### Examples

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

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

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

### Constraints

* 2 \<= n \<= 5000
* nums.length == 2 \* n
* 0 \<= nums\[i] \<= 10^4
* nums contains n + 1 unique elements and one of them is repeated exactly n times.

**Follow-up:** Can you solve it without a hash table, in O(1) extra space?

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n) worst case, returns after a handful of checks in practice
    # Space: O(1)
    def repeated_n_times(self, nums: list[int]) -> int:
        # Pigeonhole: the n copies of the answer cannot all sit 3+ slots apart,
        # so some pair at distance 1 or 2 must match. The only way no such pair
        # exists is n == 2 with the answer at both ends, hence the fallback.
        for i in range(2, len(nums)):
            if nums[i] == nums[i - 1] or nums[i] == nums[i - 2]:
                return nums[i]
        return nums[0]
```

## Complexity

| Time | Space |
| - | - |
| O(n) worst case, returns after a handful of checks in practice | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.