숫자로 이루어진 배열인 nums를 인자로 전달합니다.
숫자중에서 과반수(majority, more than a half)가 넘은 숫자를 반환해주세요.
def more_than_half(nums):
a=[]
dict={}
for num in nums:
if num in dict:
dict[num] += 1 # 딕셔너리안에 key값이 있으면 value값을 하나씩 증가시킨다.
else:
dict[num] = 1
for i in dict.values(): # value값을 뽑아낸다.
a.append(i)
b=max(a) # value값들중 큰수를 담는다.
return [k for k, v in dict.items() if v == b][0] # 담은 value값을 이용해 key값을 찾는다.
def more_than_half(nums):
a=[]
dict={}
for num in nums:
if num in dict:
dict[num] += 1
else:
dict[num] = 1
a = sorted(dict, key = lambda x : dict[x], reverse=True)
return a[0]
from collections import Counter
def more_than_half(nums):
dict = dict(Counter(nums))
a = sorted(dict, key = lambda x : dict[x], reverse=True)
return a[0]