Check if an array can be reduced to at most length K by removal of distinct elements | C # program for the above approach ; Function to check if it is possible to reduce the size of the array to K by removing the set of the distinct array elements ; Stores all distinct elements present in the array arr [ ] ; Traverse the given array ; Insert array elements into the set ; Condition for reducing size of the array to at most K ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void maxCount ( int [ ] arr , int N , int K ) { HashSet < int > st = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { st . Add ( arr [ i ] ) ; } if ( N - st . Count <= K ) { Console . Write ( " Yes " ) ; } else Console . Write ( " No " ) ; } static public void Main ( ) { int [ ] arr = { 2 , 2 , 2 , 3 } ; int K = 3 ; int N = arr . Length ; maxCount ( arr , N , K ) ; } }