class Solution { public int minSetSize(int[] arr) { Map map = new HashMap<>(); for (Integer n : arr) { if (map.containsKey(n)) map.put(n, map.get(n) + 1); else map.put(n, 1); } int[] count = new int[map.size()]; int i = 0; for (Integer c : map.values()) count[i++] = c; Arrays.sort(count); int sum = 0; i = count.length; while (sum sum += count[--i]; } return count.length - i; } }