[BaekJoon] 1302 베스트셀러 (java)

SeongWon Oh·2021년 10월 3일
0
post-thumbnail

🔗 문제 링크

https://www.acmicpc.net/problem/1302


👨🏻‍💻 작성한 코드

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class Main {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		HashMap<String, Integer> map = new HashMap<>();
		
		int num = Integer.parseInt(br.readLine());
		
		String book;
		int max = -1;
		int bookCount;
		// HashMap에 책과 책의 수의 값을 추가
		for (int i=0; i< num; i++) {
			book = br.readLine();
			if (!map.containsKey(book)) 
				bookCount = 1;
			else 
				bookCount = map.get(book)+1;
			
			map.put(book, bookCount);
			if (max < bookCount)
				max = bookCount;	
		}
		
		// 가장 많은 수의 책들을 list에 추가
		ArrayList<String> list = new ArrayList<>();
		for (String s: map.keySet()) {
			if (map.get(s)== max)
				list.add(s);
		}
		
		// 사전순으로정렬
		Collections.sort(list);
		
		// 가장 처음 책을 출력
		System.out.println(list.get(0));

	}
}
profile
블로그 이전했습니다. -> https://seongwon.dev/

0개의 댓글