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
题目描述:求一个长度为 2N
数组中重复 N
次的元素值
题目分析:很简单,把每一个出现过的不同元素进行统计,重复次数大于等于 2
的元素即为我们所求。
python
代码:
class Solution(object):
def repeatedNTimes(self, A):
"""
:type A: List[int]
:rtype: int
"""
A_length = len(A)
for i in range(A_length):
if A.count(A[i]) >= 2:
return A[i]
else:
continue
C++
代码:
class Solution {
public:
int repeatedNTimes(vector<int>& A) {
for(int i = 0; i < A.size(); i++){
if(count(A.begin(),A.end(),A[i]) >= 2){
return A[i];
}
}
}
};