# 961. N-Repeated Element in Size 2N Array

**<font color=red>难度: Easy</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/problems/n-repeated-element-in-size-2n-array/

> 内容描述

```
In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times.

Return the element repeated N times.

 

Example 1:

Input: [1,2,3,3]
Output: 3
Example 2:

Input: [2,1,2,5,3,2]
Output: 2
Example 3:

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

Note:

4 <= A.length <= 10000
0 <= A[i] < 10000
A.length is even
```

## 解题方案

> 思路 1
******- 时间复杂度: O(N)******- 空间复杂度: O(N)******


拿个字典记录一下就可以

```python
class Solution(object):
    def repeatedNTimes(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        lookup = {}
        for num in A:
            if num in lookup:
                return num
            lookup[num] = 1
```


或者一行

```python
class Solution:
    def repeatedNTimes(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        return collections.Counter(A).most_common(1)[0][0]
```

> 思路 2
******- 时间复杂度: O(N)******- 空间复杂度: O(1)******

注意一下题目条件说A的长度至少为4，我们可以把整个A看成一个循环列表，那么出现N次的那个数字肯定会相邻或者只隔一个

参考[destinynitsed](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/discuss/208333/Python-O(n)-time-and-O(1)-Space)

```python
class Solution:
    def repeatedNTimes(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        for i in range(len(A)):
            if A[i-1] == A[i] or A[i-2] == A[i]:
                return A[i]
```

> 思路 3
******- 时间复杂度: O(1)******- 空间复杂度: O(1)******

[寒神](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/discuss/208346/C%2B%2BJavaPython-4-lines-O(4)-O(1))的思路

```python
class Solution:
    def repeatedNTimes(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        while 1:
            s = random.sample(A, 2)
            if s[0] == s[1]:
                return s[0]
```




















