1. 问题描述
在给定的数组中,找到出现次数最多的数字。
出现次数相同时,返回数值最小的数字。
2. 样例
样例 1:
输入:
[1,1,2,3,3,3,4,5]
输出:
3
样例 2:
输入:
[1]
输出:
1
3. 代码
class Solution:
"""
@param array: An array.
@return: An integer.
"""
def findNumber(self, array):
# Write your code here.
count = {}
for item in sorted(array):
if item not in count.keys():
count[item] = 1
else:
count[item] += 1
return sorted(count.items(), key=lambda x: x[1], reverse=True)[0][0]
最后
以上就是无语大树最近收集整理的关于【LintCode 简单】1910. 数组中出现次数最多的值的全部内容,更多相关【LintCode内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复