import java.util.*;
class Solution {
public int solution(int[] array) {
Arrays.sort(array); // 배열을 정렬하여 같은 숫자가 연속으로 나오도록 함
int maxCount = 0; // 가장 많이 반복된 횟수를 저장
int currentCount = 1; // 현재 연속된 숫자의 반복 횟수를 저장
int maxNumber = array[0]; // 가장 많이 반복된 숫자를 저장
// 배열을 순회하면서 반복 횟수를 확인
for(int i = 1; i < array.length; i++){
if(array[i] == array[i-1]) {
// 이전 숫자와 같다면 현재 반복 횟수 증가
currentCount++;
}else{
// 새로운 숫자가 나온 경우
if(currentCount > maxCount) {
// 현재 반복 횟수가 최대 반복 횟수보다 크면 업데이트
maxCount = currentCount;
maxNumber = array[i-1]; // 가장 많이 반복된 숫자 업데이트
} else if(currentCount == maxCount) {
// 현재 반복 횟수와 최대 반복 횟수가 같다면 -1 반환
maxNumber = -1;
}
// 현재 반복 횟수 초기화
currentCount = 1;
}
}
// 마지막 숫자의 반복 횟수 확인
if(currentCount > maxCount) {
// 마지막 숫자의 반복 횟수가 최대 반복 횟수보다 크면 업데이트
maxNumber = array[array.length -1];
} else if(currentCount == maxCount) {
// 마지막 숫자의 반복 횟수와 최대 반복 횟수가 같다면 -1 반환
maxNumber = -1;
}
return maxNumber; // 결과 반환
}