Minimum changes required to make all element in an array equal | Function to count of minimum changes required to make all elements equal ; Store the count of each element as key value pair in unordered map ; Find the count of maximum occurring element ; Return count of all element minus count of maximum occurring element ; Driver code ; Function call
function minChanges ( arr , n ) { var umap = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( umap . has ( arr [ i ] ) ) { umap . set ( arr [ i ] , umap . get ( arr [ i ] ) + 1 ) ; } else { umap . set ( arr [ i ] , 1 ) ; } } var maxFreq = 0 ; umap . forEach ( ( values , keys ) => { maxFreq = Math . max ( maxFreq , values ) ; } ) ; return n - maxFreq ; } var arr = [ 2 , 3 , 3 , 4 ] ; var n = arr . length ; document . write ( minChanges ( arr , n ) + ' ' ) ;