public int solution(int k, int[] tangerine) {
Map<Integer, Integer> sizeMap = new HashMap<>();
for (int size : tangerine) {
sizeMap.put(size, sizeMap.getOrDefault(size, 0) + 1);
}
List<Integer> keyList = new ArrayList<>(sizeMap.keySet());
keyList.sort(Comparator.comparingInt(sizeMap::get).reversed());
int answer = 0;
for (int i = 0; i < keyList.size() && k > 0; i++) {
int size = keyList.get(i);
int count = sizeMap.get(size);
answer++;
k -= count;
}
return answer;
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/138476