Find the minimum number of operations required to make all array elements equal | Function to return the minimum operations required to make all array elements equal ; To store the frequency of all the array elements ; Traverse through array elements and update frequencies ; To store the maximum frequency of an element from the array ; Traverse through the map and find the maximum frequency for any element ; Return the minimum operations required ; Driver code
function minOperations ( arr , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) } else { mp . set ( arr [ i ] , 1 ) } } let maxFreq = Number . MIN_SAFE_INTEGER ; for ( let x of mp ) maxFreq = Math . max ( maxFreq , x [ 1 ] ) ; return ( n - maxFreq ) ; } let arr = [ 2 , 4 , 6 ] ; let n = arr . length ; document . write ( minOperations ( arr , n ) ) ;