import java.util.*;
class Solution {
public int solution(int[] array) {
int maxCount = 0;
int answer = 0;
Map<Integer, Integer> map = new HashMap<>();
for(int number : array) {
int count = map.getOrDefault(number, 0) + 1;
if(count > maxCount) {
maxCount = count;
answer = number;
}
else if(count == maxCount) {
answer = -1;
}
map.put(number, count);
}
return answer;
}
}
import java.util.*;
class Solution {
public int solution(int[] array) {
HashMap<Integer, Integer> nH = new HashMap<>();
for (int x : array) {
nH.put(x, nH.getOrDefault(x, 0) + 1);
}
int max = 0;
for (int key : nH.keySet()) {
if (nH.get(key) > max) max = nH.get(key);
}
ArrayList<Integer> al = new ArrayList<>();
for (int key : nH.keySet()) {
if (nH.get(key) == max) al.add(key);
}
if (al.size() > 1) return -1;
return al.get(0);
}
}
https://school.programmers.co.kr/learn/courses/30/lessons/120812