package leetcode;

/**
 * https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/
 */
public class Problem1481 {
    public int findLeastNumOfUniqueInts(int[] arr, int k) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1481 prob = new Problem1481();
        System.out.println(prob.findLeastNumOfUniqueInts(new int[]{5,5,4}, 1)); // 1
        System.out.println(prob.findLeastNumOfUniqueInts(new int[]{5,5,4}, 2)); // 1
        System.out.println(prob.findLeastNumOfUniqueInts(new int[]{5,5,4,6}, 2)); // 1
        System.out.println(prob.findLeastNumOfUniqueInts(new int[]{5,5,4,6}, 3)); // 1
        System.out.println(prob.findLeastNumOfUniqueInts(new int[]{5,5,4,4,6}, 4)); // 1
        System.out.println(prob.findLeastNumOfUniqueInts(new int[]{4,3,1,1,3,3,2}, 3)); // 2
    }
}

@@ -12,16 +12,145 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Comparator;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A
 /**%0A * h
@@ -331,657 +331,758 @@
     
-// TODO%0A        return 0;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1481 prob = new Problem1481();%0A        System.out.println(prob.findLeastNumOfUniqueInts(new int%5B%5D%7B5,5,4%7D, 1)); // 1%0A        System.out.println(prob.findLeastNumOfUniqueInts(new int%5B%5D%7B5,5,4%7D, 2)); // 1%0A        System.out.println(prob.findLeastNumOfUniqueInts(new int%5B%5D%7B5,5,4,6%7D, 2)); // 1%0A        System.out.println(prob.findLeastNumOfUniqueInts(new int%5B%5D%7B5,5,4,6%7D, 3)); // 1%0A        System.out.println(prob.findLeastNumOfUniqueInts(new int%5B%5D%7B5,5,4,4,6%7D, 4)); // 1%0A        System.out.println(prob.findLeastNumOfUniqueInts(new int%5B%5D%7B4,3,1,1,3,3,2%7D, 3)); // 2
+Map%3CInteger, Integer%3E map = new HashMap%3C%3E();%0A        for (int a : arr) %7B%0A            map.put(a, map.getOrDefault(a, 0) + 1);%0A        %7D%0A        List%3CMap.Entry%3CInteger, Integer%3E%3E sorted = new ArrayList%3C%3E(map.entrySet());%0A        sorted.sort(Comparator.comparingInt(Map.Entry::getValue));%0A        int i = k;%0A        int j = 0;%0A        while (i %3E 0) %7B%0A            Map.Entry%3CInteger, Integer%3E entry = sorted.get(j);%0A            if (i %3E= entry.getValue()) %7B%0A                map.remove(entry.getKey());%0A                i -= entry.getValue();%0A            %7D else %7B%0A                int newCount = entry.getValue() - i;%0A                map.put(entry.getKey(), newCount);%0A                break;%0A            %7D%0A            j++;%0A        %7D%0A        return map.size();
 %0A   

Solve problem 1481