src/main/java/com/leetcode/greedy/medium/ReduceArraySizeToTheHalf_1338.java
public int minSetSize(int[] arr) {
      Map<Integer, Integer> counter = new HashMap<>();

      for (int num : arr) {
        counter.merge(num, 1, (prev, b) -> prev + 1);
      }

      int[] timesOfOccurrences = new int[arr.length + 1];

      for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
        timesOfOccurrences[entry.getValue()]++;
      }

      int result = 0;

      for (int i = timesOfOccurrences.length - 1, goal = 0; goal < arr.length / 2; i--) {
        while (timesOfOccurrences[i] > 0 && goal < arr.length / 2) {
          goal += i;
          result++;
          timesOfOccurrences[i]--;
        }
      }

      return result;
    }
/*
     O(n) Runtime: 27 ms, faster than 87.96% of Java online submissions for Reduce Array Size to The Half.
     O(n) Memory Usage: 48.7 MB, less than 81.94% of Java online submissions for Reduce Array Size to The Half.
    */
O(n) Runtime: 27 ms, faster than 87.96% of Java online submissions for Reduce Array Size to The Half.
O(n) Memory Usage: 48.7 MB, less than 81.94% of Java online submissions for Reduce Array Size to The Half.
O ( n ) Runtime : 27 ms faster than 87 . 96% of Java online submissions for Reduce Array Size to The Half . O ( n ) Memory Usage : 48 . 7 MB less than 81 . 94% of Java online submissions for Reduce Array Size to The Half .
public int minSetSize(int[] arr) {
      Map<Integer, Integer> counter = new HashMap<>();
      for (int num : arr) {
        counter.merge(num, 1, (prev, b) -> prev + 1);
      }
      int[] timesOfOccurrences = new int[arr.length + 1];
      for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
        timesOfOccurrences[entry.getValue()]++;
      }
      int result = 0;
      for (int i = timesOfOccurrences.length - 1, goal = 0; goal < arr.length / 2; i--) {
        while (timesOfOccurrences[i] > 0 && goal < arr.length / 2) {
          goal += i;
          result++;
          timesOfOccurrences[i]--;
        }
      }
      return result;
    }
public int minSetSize ( int [ ] arr ) { Map < Integer , Integer > counter = new HashMap < > ( ) ; for ( int num : arr ) { counter . merge ( num , 1 , ( prev , b ) -> prev + 1 ) ; } int [ ] timesOfOccurrences = new int [ arr . length + 1 ] ; for ( Map . Entry < Integer , Integer > entry : counter . entrySet ( ) ) { timesOfOccurrences [ entry . getValue ( ) ] ++ ; } int result = 0 ; for ( int i = timesOfOccurrences . length - 1 , goal = 0 ; goal < arr . length / 2 ; i -- ) { while ( timesOfOccurrences [ i ] > 0 && goal < arr . length / 2 ) { goal += i ; result ++ ; timesOfOccurrences [ i ] -- ; } } return result ; }
O(n) Runtime: 27 ms, faster than 87.96% of Java online submissions for Reduce Array Size to The Half.
O ( n ) Runtime : 27 ms faster than 87 . 96% of Java online submissions for Reduce Array Size to The Half .