숫자로 이루어진 배열인 nums를 인자로 전달합니다.
숫자중에서 과반수(majority, more than a half)가 넘은 숫자를 반환해주세요.
예를 들어,
nums = [3,2,3]
return 3
nums = [2,2,1,1,1,2,2]
return 2
from collections import Counter
def more_than_half(nums):
# 아래 코드를 입력해주세요.
num_count = {value: key for key, value in Counter(nums).items()}
return num_count[max(num_count.keys())]
Counter
메소드를 사용하여 리스트에서 개수를 구하는데 나중을 위해 key
와 value
를 뒤집었다.
key
중에 가장 큰 값을 찾아 dictionary
에서 value
를 리턴한다.
간만에 쉬운문제 🤤