Check if frequency of each element in given array is unique or not | Function to check whether the frequency of elements in array is unique or not . ; Freq map will store the frequency of each element of the array ; Store the frequency of each element from the array ; Check whether frequency of any two or more elements are same or not . If yes , return false ; Return true if each frequency is unique ; Given array arr [ ] ; Function call ; Print the result
function checkUniqueFrequency ( arr , n ) { let freq = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( freq . has ( arr [ i ] ) ) { freq . set ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . set ( arr [ i ] , 1 ) ; } } let uniqueFreq = new Set ( ) ; for ( let [ key , value ] of freq . entries ( ) ) { if ( uniqueFreq . has ( value ) ) return false ; else uniqueFreq . add ( value ) ; } return true ; } let arr = [ 1 , 1 , 2 , 5 , 5 ] ; let n = arr . length ; let res = checkUniqueFrequency ( arr , n ) ; if ( res ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;