Print numbers in descending order along with their frequencies | Java program to print the elements in descending along with their frequencies ; Function to print the elements in descending along with their frequencies ; Sorts the element in decreasing order ; traverse the array elements ; Prints the number and count ; Prints the last step ; Driver Code
import java . util . * ; class GFG { static void printElements ( int a [ ] , int n ) { Arrays . sort ( a ) ; a = reverse ( a ) ; int cnt = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] != a [ i + 1 ] ) { System . out . print ( a [ i ] + " ▁ occurs ▁ " + cnt + " timesNEW_LINE"); cnt = 1 ; } else cnt += 1 ; } System . out . print ( a [ n - 1 ] + " ▁ occurs ▁ " + cnt + " timesNEW_LINE"); } static int [ ] reverse ( int a [ ] ) { int i , n = a . length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 1 , 1 , 2 , 3 , 4 , 9 , 9 , 10 } ; int n = a . length ; printElements ( a , n ) ; } }