🪄내가 한 solution

  • sorted(iterable, key, reverse)
    • iterable : 반복 가능한 iterable 자료형 입력
    • key : 정렬을 목적으로 하는 함수를 넣음
    •  reverse : False(오름차순, default), True(내림차순)
def solution(array):
    count = {}
    max_num = 0
    
    # for 반복문으로 입력 list 순회
    for i in array:
    	# 딕셔너리에 현재 key가 있다면 value에 1 더하기
        if i in count: 
            count[i] += 1
        # 딕셔너리에 현재 key가 할당되어 있지 않다면 1 할당
        else: 
            count[i] = 1
            
    # 딕셔너리의 value 값 기준으로 내림차순 정렬
    result = sorted(count.items(), key=lambda x: x[1], reverse=True)
    
    # 만약 result가 1개라면 그 값 바로 출력
    if len(result) <= 1: return result[0][0]
    
    # 최빈값이 여러개라면 
    # result(list)의 첫번째 tuple의 1번째 인덱스값과 두번째 tuple의 1번째 인덱스 값 같음
    # 그렇다면 -1 출력
    return result[0][0] if result[0][1] != result[1][11] else -1

+ Recent posts