반응형

자료구조...,,,  탐색....,,


범위가 작음.... 문제 생길게 없다.

해쉬맵 알고 있다면 쉽게 풀 수 있음


풀이

1. 해쉬맵을 통해 이름과 횟수를 저장한다. (key, value) 처럼

2. 해쉬맵을 전체 탐색하여 가장 큰 value를 max에 저장한다.

3. 해쉬맵의 key들을 리스트로 옮긴다. ( 사전 출력을 하기 위해)

4. 리스트를 정렬한다.

5. 리스트에서 value값이 max값인 key를 하나만 출력한다. 


소스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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 {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        HashMap<String,Integer> map = new HashMap<String,Integer>();
        String str = new String();
        for(int i=0; i<N; i++){
            str = br.readLine();
            if(map.containsKey(str)){
                map.replace(str, map.get(str)+1);
            }
            else{
                map.put(str, 1);
            }
        }
        int max = 0;
        for(String a : map.keySet()){
            max = Math.max(max, map.get(a));
        }
        
        ArrayList<String> al = new ArrayList<String>(map.keySet());
        Collections.sort(al);
        for(String a : al){
            if(map.get(a)==max){
                System.out.println(a);
                break;
            }
        }
    }
}
cs


반응형

+ Recent posts