Check if two Integer are anagrams of each other | Javascript implementation of the approach ; Function to update the frequency array such that freq [ i ] stores the frequency of digit i in n ; While there are digits left to process ; Update the frequency of the current digit ; Remove the last digit ; Function that returns true if a and b are anagarams of each other ; To store the frequencies of the digits in a and b ; Update the frequency of the digits in a ; Update the frequency of the digits in b ; Match the frequencies of the common digits ; If frequency differs for any digit then the numbers are not anagrams of each other ; Driver code
const TEN = 10 ; function updateFreq ( n , freq ) { while ( n ) { let digit = n % TEN ; freq [ digit ] ++ ; n = parseInt ( n / TEN ) ; } } function areAnagrams ( a , b ) { let freqA = new Array ( TEN ) . fill ( 0 ) ; let freqB = new Array ( TEN ) . fill ( 0 ) ; updateFreq ( a , freqA ) ; updateFreq ( b , freqB ) ; for ( let i = 0 ; i < TEN ; i ++ ) { if ( freqA [ i ] != freqB [ i ] ) return false ; } return true ; } let a = 240 , b = 204 ; if ( areAnagrams ( a , b ) ) document . write ( " " ) ; else document . write ( " " ) ;