<span>leetcode-961 N-Repeated Element in Size 2N Array</span>

题目:

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.

 实例输出:

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

本题就是一个简单的字典统计,然后找到出现了n次的数字输出就行了。

 1 class Solution:
 2     def repeatedNTimes(self, A: List[int]) -> int:
 3         dic = {}
 4         for i in A:
 5             if i in dic:
 6                 dic[i] += 1
 7             else:
 8                 dic[i] = 1
 9         for i in dic:
10             if dic[i] * 2 == len(A):
11                 return i

 

全部评论

相关推荐

11-15 13:12
已编辑
门头沟学院 Java
斯卡蒂味的鱼汤:知道你不会来数马,就不捞你😂最近数马疯狂扩招,招聘要求挺低的,你能力肯定够,应该就是因为太强了,知道你不会来才不捞你
投递腾讯云智研发等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务