접근방법

  1. 숫자마다 갯수를 샌다
  2. 여기서는 매우 큰수 이기때문에 배열과 같은 것 보다는 숫자와 횟수를 저장하는 Map 구조를 사용했다.

입 / 출력

입력 4
1
1
2
2
4
2
2
1
1
1
1
6
1
2
1
2
1
2
5
1
2
1
2
1
1
1000000000
출력 1 1 1 1 1 1000000000

 

코드

더보기

내가 작성한 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class 카드 {
    public static void main(String[] arg) throws NumberFormatException, IOException {
        HashMap<Long, Integer> map = new HashMap<>();
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());

        for (int i = 0; i < n; i++) {
            Long m = Long.parseLong(reader.readLine());

            if (map.get(m) != null) {
                map.put(m, map.get(m) + 1);
            } else
                map.put(m, 1);
        }
        List<Map.Entry<Long, Integer>> list = new ArrayList<>(map.entrySet());

        Long value = Long.MIN_VALUE;
        int count = 0;
        for (Map.Entry<Long, Integer> item : list) {
            if (item.getValue() == count) {
                value = Math.min(value, item.getKey());
            } else if (item.getValue() > count) {
                value = item.getKey();
                count = item.getValue();
            }

        }
        System.out.println(value);
    }
}

 

클린코드로 변환 요청한 공부용 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.OptionalLong;

public class CardFrequency {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        Map<Long, Integer> cardFrequencyMap = new HashMap<>();

        // 카드 번호를 입력받고 빈도를 계산하는 부분
        for (int i = 0; i < n; i++) {
            Long cardNumber = Long.parseLong(reader.readLine());
            cardFrequencyMap.put(cardNumber, cardFrequencyMap.getOrDefault(cardNumber, 0) + 1);
        }

        // 가장 빈도가 높은 카드와, 빈도가 같을 경우 가장 작은 값을 찾는 부분
        OptionalLong mostFrequentCard = cardFrequencyMap.entrySet()
                .stream()
                .max((entry1, entry2) -> {
                    int freqCompare = entry1.getValue().compareTo(entry2.getValue());
                    if (freqCompare == 0) {
                        return entry1.getKey().compareTo(entry2.getKey());
                    }
                    return freqCompare;
                })
                .map(Map.Entry::getKey)
                .stream()
                .mapToLong(Long::longValue)
                .findFirst();

        mostFrequentCard.ifPresent(System.out::println);
    }
}

'개인 공부 > 백준' 카테고리의 다른 글

2470번 두 용액 ( JAVA )  (2) 2024.09.22
1300번 K번째 수 ( JAVA )  (0) 2024.09.19
11401번 이항 계수 3  (1) 2024.09.08
2156번 포도주 시식 ( JAVA )  (1) 2024.09.04
2580번 스도쿠 ( JAVA )  (1) 2024.09.03

+ Recent posts