Check if frequency of each element in given array is unique or not | Java code for the above approach ; 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 ; Driver Code ; Given array arr [ ] ; Function call ; Print the result
import java . util . * ; class GFG { static boolean checkUniqueFrequency ( int arr [ ] , int n ) { HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . containsKey ( arr [ i ] ) ) { freq . put ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . put ( arr [ i ] , 1 ) ; } } HashSet < Integer > uniqueFreq = new HashSet < Integer > ( ) ; for ( Map . Entry < Integer , Integer > i : freq . entrySet ( ) ) { if ( uniqueFreq . contains ( i . getValue ( ) ) ) return false ; else uniqueFreq . add ( i . getValue ( ) ) ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 5 , 5 } ; int n = arr . length ; boolean res = checkUniqueFrequency ( arr , n ) ; if ( res ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } }