输入包含两行,第一行包含一个整数n,代表数组长度,第二行包含n个数,代表数组arr
。
输出一个整数,代表出现次数大于一半的数,如果没有这样的数,请输出‘-1“。
5 11 7 5 7 7
7
4 2 2 3 3
-1
时间复杂度,额外空间复杂度
。
def max_count(arr):
count = {}
for i in set(arr):
count[i] = arr.count(i)
return count
num = input()
arr = input().split()
map = max_count(arr)
l = sorted(map.items(),key = lambda x:x[1])
if l[-1][-1] > len(arr)/2:
print(l[-1][0])
else:
print(-1)