Check if the sum of distinct digits of two integers are equal | Function to return the sum of distinct digits of a number ; Take last digit ; If digit has not been used before ; Set digit as used ; Remove last digit ; Function to check whether the sum of distinct digits of two numbers are equal ; Driver code
function distinctDigitSum ( n ) { var used = Array ( 10 ) . fill ( false ) ; var sum = 0 ; while ( n > 0 ) { var digit = n % 10 ; if ( ! used [ digit ] ) { used [ digit ] = true ; sum += digit ; } n = parseInt ( n / 10 ) ; } return sum ; } function checkSum ( m , n ) { var sumM = distinctDigitSum ( m ) ; var sumN = distinctDigitSum ( n ) ; if ( sumM == sumN ) return " " ; return " " ; } var m = 2452 , n = 9222 ; document . write ( checkSum ( m , n ) ) ;