Minimum number of distinct elements after removing m items | Java program for Minimum number of distinct elements after removing m items ; Function to find distintc id 's ; Store the occurrence of ids ; Start removing elements from the beginning ; Remove if current value is less than or equal to mi ; Return the remaining size ; Driver method to test above function
import java . util . HashMap ; import java . util . Map ; import java . util . Map . Entry ; public class DistinctIds { static int distinctIds ( int arr [ ] , int n , int mi ) { Map < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; int count = 0 ; int size = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( arr [ i ] ) == false ) { m . put ( arr [ i ] , 1 ) ; size ++ ; } else m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } for ( Entry < Integer , Integer > mp : m . entrySet ( ) ) { if ( mp . getKey ( ) <= mi ) { mi -= mp . getKey ( ) ; count ++ ; } else return size - count ; } return size - count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 , 2 , 3 , 3 } ; int m = 3 ; System . out . println ( distinctIds ( arr , arr . length , m ) ) ; } }