Find maximum number that can be formed using digits of a given number | Function to print the maximum number ; hashed array to store count of digits ; Converting given number to string ; Updating the count array ; result is to store the final number ; Traversing the count array to calculate the maximum number ; return the result ; Driver program to test above function
function printMaxNum ( num ) { let count = new Array ( 10 ) ; for ( let i = 0 ; i < count . length ; i ++ ) { count [ i ] = 0 ; } let str = num . toString ( ) ; for ( let i = 0 ; i < str . length ; i ++ ) count [ str [ i ] - ' ' ] ++ ; let result = 0 , multiplier = 1 ; for ( let i = 0 ; i <= 9 ; i ++ ) { while ( count [ i ] > 0 ) { result = result + ( i * multiplier ) ; count [ i ] -- ; multiplier = multiplier * 10 ; } } return result ; } let num = 38293367 ; document . write ( printMaxNum ( num ) ) ;