Check if an array can be reduced to at most length K by removal of distinct elements | Java 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
import java . util . HashSet ; public class GFG { static void maxCount ( int arr [ ] , int N , int K ) { HashSet < Integer > st = new HashSet < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { st . add ( arr [ i ] ) ; } if ( N - st . size ( ) <= K ) { System . out . println ( " Yes " ) ; } else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 2 , 3 } ; int K = 3 ; int N = arr . length ; maxCount ( arr , N , K ) ; } }