题目
算法思想
依题意求出输入的整数列表中出现次数最多的数,故使用count函数直接统计各个数出现的次数,接着遍历count_L列表,若列表已存在这个数,break继续求下一个数出现的次数,否则使用append函数将其放到count_L列表中,统计结束,使用sort函数对count_L列表进行排序(从大到小和同等条件下输出数小的),最后列表第一个元素就是所求的出现次数最多的数
源代码
n = int(input())
L = list(map(int,input().split()))
count_L = []
for i in range(n):
count = L.count(L[i])
for j in count_L:
if j[0] == L[i]:
break
else:
count_L.append([L[i],count])
count_L.sort(key=lambda x:(-x[1],x[0]))
print(count_L[0][0])