[LeetCode]#961. N-Repeated Element in Size 2N Array

Fatboy Slim
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:

  1. Use Counter function in A
  2. Create key and value
  3. if value =A size /2
  4. 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:

--

--

Fatboy Slim
Fatboy Slim

Written by Fatboy Slim

Interesting in any computer science.

No responses yet