[LeetCode]#961. N-Repeated Element in Size 2N Array
1 min readMay 7, 2020
Environment: Python 3.7
Key technique: from collections import Counter
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
Analysis:
- Use Counter function in A
- Create key and value
- if value =A size /2
- return key
Solution:
from collections import Counter
class Solution:
def repeatedNTimes(self, A):
count_A=Counter(A)
ans=0
l=len(count_A)
for i in count_A:
if count_A[i] == len(A)/2:
ans=i
return ans
Submissions: