[백준(JAVA)] 2592번: 대표값

세하·2025년 4월 22일

[백준] 문제풀이

목록 보기
41/94
post-thumbnail

문제

✔ 난이도 - Bronze 2

설명

HashMap에 대해 알고만 있다면 정말 쉬운 문제
https://velog.io/@seha01130/HashMap이란-메소드-종류-정리

풀이

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        HashMap<Integer, Integer> hm = new HashMap<>();
        int sum = 0;
        for(int i = 0 ; i< 10; i++){
            int inputInt = Integer.parseInt(br.readLine());
            sum += inputInt;
            if (hm.containsKey(inputInt)){
                hm.replace(inputInt, hm.get(inputInt) + 1);
            } else {
                hm.put(inputInt, 1);
            }
        }
        int avg = sum / 10;
        //System.out.println(hm);

        Set<Integer> keyset = hm.keySet();
        Iterator<Integer> iterator = keyset.iterator();

        int mostValue = 0;
        int mostKey = 0;
        while(iterator.hasNext()){
            int thisKey = iterator.next();
            int thisValue = hm.get(thisKey);
            if (thisValue > mostValue){
                mostValue = thisValue;
                mostKey = thisKey;
            }
        }

        sb.append(avg).append("\n").append(mostKey);
        System.out.println(sb);
    }
}

0개의 댓글