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

``````class Solution {
public:
int repeatedNTimes(vector<int>& A) {
int n = A.size();
unordered_map<int, int> numCnt;
for (int num : A) {
if (++numCnt[num] > 1) return num;
}
return -1;
}
};
``````

``````class Solution {
public:
int repeatedNTimes(vector<int>& A) {
for (int i = 2; i < A.size(); ++i) {
if (A[i] == A[i - 1] || A[i] == A[i - 2]) {
return A[i];
}
}
return A[0];
}
};
``````

Github 同步地址:

https://github.com/grandyang/leetcode/issues/961

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

https://leetcode.com/problems/n-repeated-element-in-size-2n-array/discuss/208317/C%2B%2B-2-lines-O(4)-or-O-(1)

https://leetcode.com/problems/n-repeated-element-in-size-2n-array/discuss/208563/JavaC%2B%2BPython-O(1)-Solution

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation