def more_than_half(nums):
result = []
for i in nums:
if nums.count(i) >= len(nums) / 2:
result.append(i)
return result[0]
# 요소 모두가 같으므로 1개만 뽑아옴.
print(more_than_half([3, 2, 3]))
난이도가 높지 않아서 금방 해결했다.
def more_than_half(nums):
nums.sort()
# 순서 상관 없으므로 일단 정렬
to_dict = {i : nums.count(i) for i in nums}
# list comprehension으로 리스트를 딕셔너리화하고 각 요소가 몇 개인지 확인
return max(to_dict.keys(), key=lambda k : to_dict[k])
# lambda 이용하여 가장 큰 키의 값을 추출
print(more_than_half([0,1,2,3,0,0,0,0,0]))