Reduce Array Size to At Most Half
Problem statement
Choose a set of distinct integer values and remove every occurrence of each chosen value from values. Return the minimum number of chosen values needed so that the remaining array has at most half of its original length, using integer floor for the allowed remaining length.
Function
minValuesToRemoveHalf(values: int[]) → intExamples
Example 1
values = [3,3,3,3,5,5,5,2,2,7]return = 2Removing all 3s and all 5s removes seven elements.
Example 2
values = [7,7,7,7]return = 1One selected value removes the whole array.
Constraints
1 <= values.length <= 200000- Each value fits in a signed 32-bit integer.