Check if an array can be reduced to at most length K by removal of distinct elements | 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
function maxCount ( arr , N , K ) { let st = new Set ( ) ; for ( let i = 0 ; i < N ; i ++ ) { st . add ( arr [ i ] ) ; } if ( N - st . size <= K ) { document . write ( " " ) ; } else document . write ( " " ) ; } let arr = [ 2 , 2 , 2 , 3 ] ; let K = 3 ; let N = arr . length maxCount ( arr , N , K ) ;