输出数组中出现次数最多且值最大的数字----python
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)