Reduce Array Size to The Half
Solutions
Approach – Using Hash Table
public class Solution
{
public int MinSetSize(int[] arr)
{
int mid = arr.Length / 2;
Dictionary<int, int> map = new Dictionary<int, int>();
for (int i = 0; i < arr.Length; i++)
{
var key = arr[i];
if (map.ContainsKey(key))
{
map[key]++;
}
else
{
map[key] = 1;
}
}
int count = 0;
int sum = 0;
foreach (var item in map.OrderByDescending(x=>x.Value))
{
sum += item.Value;
count++;
if (sum >= mid) return count;
}
return 0;
}
}Last updated