FindMinNumOfUniqueIntegers.cs

CSharp · Console

/Program.cs

using System;
using System.Linq;
using System.Collections.Generic;
					
public class Program
{
	public static int FindMinNumOfUniqueIntegers(int[] arr, int k) {
        var map = new Dictionary<int, int>();
        for (int i = 0; i < arr.Length; i++)
        {
            if (!map.ContainsKey(arr[i]))
            {
                map[arr[i]] = 0;
            }
            map[arr[i]] += 1;
        }

        int count = 0;
        var sortedFreq = map.OrderBy(x => x.Value).Select(x => x.Value);
        foreach (int freq in sortedFreq)
        {
            if (freq > k)
            {
                break;
            }

            k -= freq;
            count++;
        }

        return sortedFreq.Count() - count;
    }
	
	public static void Main()
	{
		Console.WriteLine("UniLecs");
		
		// tests
		Console.WriteLine(FindMinNumOfUniqueIntegers(new int[] { 5, 5, 4 }, 1).ToString()); // 1
		Console.WriteLine(FindMinNumOfUniqueIntegers(new int[] { 4, 3, 1, 1, 3, 3, 2 }, 2).ToString()); // 2
	}
}
;