https://www.acmicpc.net/problem/11652
public class boj_11652 {
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.parseInt(r.readLine());
Long mostFrequent = IntStream.range(0, count)
.mapToObj(i -> {
try {
return Long.parseLong(r.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.groupingBy(i -> i, Collectors.counting()))
.entrySet().stream()
.max(Comparator.comparing(Map.Entry<Long, Long>::getValue)
.thenComparing(Map.Entry<Long,Long>::getKey,Comparator.reverseOrder())) // 빈도가 같으면 키(숫자)를 기준으로 비교
.map(Map.Entry::getKey)
.orElse(null);
System.out.println(mostFrequent);
}
}
public class boj_11652 {
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.parseInt(r.readLine());
/**
* 첫 번째 스트림: 각 숫자의 빈도를 계산
*/
Map<Long, Long> frequencyMap = IntStream.range(0, count)
.mapToObj(i -> {
try {
return Long.parseLong(r.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.groupingBy(i -> i, Collectors.counting()));
/**
* 두 번째 스트림: 계산된 빈도를 기반으로 가장 많이 중복된 숫자 찾기
*/
Long mostFrequent = frequencyMap.entrySet().stream()
.max(Comparator.comparing(Map.Entry<Long, Long>::getValue)
.thenComparing(Map.Entry<Long, Long>::getKey, Comparator.reverseOrder()))
.map(Map.Entry::getKey)
.orElse(null);
System.out.println(mostFrequent);
}
}