输出数组中出现次数最多且值最大的数字----python
2024-10-08 00:26:45
class Solution():
#求最多的数
def find_max(self,list):
num = 0
for i in list:
print(i)
if list.count(i) > num:
num = list.count(i)
value = i
return value #求最多且最大的数
def find_most_num(self,list):
num = 0
most = 0
for i in list:
print(i)
if list.count(i) >= num:
if i > most:
most = i
num = list.count(i)
return most
#另一种算法
def find_most_dict(self,list):
dict = {}
for i in list:
if i in dict.keys():
dict[i] += 1
else:
dict[i] = 1
print(dict)
max = 0
for k,v in dict.items():
if v >= max:
value = k
max = v
if k > value:
value = k
return value if __name__ =='__main__':
list = [1,2,8,13,3,4,8,8,2,5,3,1,2,4,5,5,2,8,19,8,2]
solution = Solution()
# result = solution.find_max(list)
# print('==============')
# print(result)
print('=====================================')
#result2 = solution.find_most_num(list)
print('==============')
#print(result2)
result3 = solution.find_most_dict(list)
print('==============')
print(result3)
最新文章
- Socket聊天程序——客户端
- EasyUI刚加载时候Window窗体自动弹出的解决办法
- 深入instanceof
- CocoaPods安装及使用详情
- js高级群的一些整理6月
- python窗体——pyqt初体验
- ie8中支持 password 的 placeholder
- JsRender系列demo(9)自定义函数
- what is WLAN ? when and why we need use it ?
- NFC(9)NDEF文本格式规范及读写示例(解析与封装ndef 文本)
- 使用 testng.xml 参数化
- 《Effective C++》Item2:尽量以const,enum,inline替换#define
- 【Lucene】近实时搜索
- Chapter 1 First Sight——9
- 【Javascript语言精粹】笔记摘要
- 菜鸟级Git GitHub创建仓库
- BZOJ1412[ZJOI2009]狼和羊的故事——最小割
- P1036 选数 题解
- 如何将web项目部署到Ubuntu服务器上
- mysql分享二-防止sql注入