최빈수 구하기

최민수·2023년 7월 13일
0

알고리즘

목록 보기
67/94
from collections import defaultdict

import sys
sys.stdin = open("input.txt", "r")

T = int(input())
# 여러개의 테스트 케이스가 주어지므로, 각각을 처리합니다.
for test_case in range(1, T + 1):
    test_num = int(input())
    scores = list(map(int, input().split()))
    scoresSet = set(scores)
    scoreMap = defaultdict(int)
    mfs, mfs_cnt = scores[0], 1

    for item in scoresSet:
        if mfs_cnt < scores.count(item):
            mfs = item
            mfs_cnt = scores.count(item)

    print("#" + str(test_case) + " " + str(mfs))

D2

쭉 나와있는 값들의 최빈수를 구하는 아주 간단한 문제였다.
Set으로 바꾼 뒤 순회하면서 업데이트 해주는 방식으로 간단하게 풀렸다.


출처: https://swexpertacademy.com/main/talk/solvingClub/problemView.do?solveclubId=AYj2mga6ZewDFASl&contestProbId=AV13zo1KAAACFAYh&probBoxId=AYj2nEQ6ZfkDFASl&type=PROBLEM&problemBoxTitle=%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98+Track+%28%EB%82%9C%EC%9D%B4%EB%8F%84+%EC%A4%91%29&problemBoxCnt=5

profile
CS, 개발 공부기록 🌱

0개의 댓글