# Print numbers in descending order along with their frequencies

Given an array arr, the task is to print the elements of the array in descending order along with their frequencies.

Examples:

Input: arr[] = {1, 3, 3, 3, 4, 4, 5}
Output: 5 occurs 1 times
4 occurs 2 times
3 occurs 3 times
1 occurs 1 times

Input: arr[] = {1, 1, 1, 2, 3, 4, 9, 9, 10}
Output: 10 occurs 1 times
9 occurs 2 times
4 occurs 1 times
3 occurs 1 times
2 occurs 1 times
1 occurs 3 times

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive approach: Use some Data-Structure (e.g. multiset) which stores elements in decreasing order and then print the elements one by one with it’s count and then erase it from the Data-structure. The time complexity will be O(N log N) and the auxiliary space will be O(N) for the Data-structure used.

Below is the implementation of the above approach:

 `// C++ program to print the elements in ` `// descending along with their frequencies ` `#include ` `using` `namespace` `std; ` ` `  `// Function to print the elements in descending ` `// along with their frequencies ` `void` `printElements(``int` `a[], ``int` `n) ` `{ ` ` `  `    ``// A multiset to store elements in decreasing order ` `    ``multiset<``int``, greater<``int``> > ms; ` ` `  `    ``// Insert elements in the multiset ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``ms.insert(a[i]); ` `    ``} ` ` `  `    ``// Print the elements along with their frequencies ` `    ``while` `(!ms.empty()) { ` ` `  `        ``// Find the maximum element ` `        ``int` `maxel = *ms.begin(); ` ` `  `        ``// Number of times it occurs ` `        ``int` `times = ms.count(maxel); ` ` `  `        ``cout << maxel << ``" occurs "` `<< times << ``" times\n"``; ` ` `  `        ``// Erase the maxel ` `        ``ms.erase(maxel); ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `a[] = { 1, 1, 1, 2, 3, 4, 9, 9, 10 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a[0]); ` `    ``printElements(a, n); ` `    ``return` `0; ` `} `

Output:
```10 occurs 1 times
9 occurs 2 times
4 occurs 1 times
3 occurs 1 times
2 occurs 1 times
1 occurs 3 times
```

Efficient Approach: Sort the array in descending order and then start printing the elements from the beginning along with their frequencies.

Below is the implementation of the above approach:

 `// C++ program to print the elements in ` `// descending along with their frequencies ` `#include ` `using` `namespace` `std; ` ` `  `// Function to print the elements in descending ` `// along with their frequencies ` `void` `printElements(``int` `a[], ``int` `n) ` `{ ` ` `  `    ``// Sorts the element in decreasing order ` `    ``sort(a, a + n, greater<``int``>()); ` `    ``int` `cnt = 1; ` ` `  `    ``// traverse the array elements ` `    ``for` `(``int` `i = 0; i < n - 1; i++) { ` ` `  `        ``// Prints the number and count ` `        ``if` `(a[i] != a[i + 1]) { ` `            ``cout << a[i] << ``" occurs "` `<< cnt << ``" times\n"``; ` `            ``cnt = 1; ` `        ``} ` `        ``else` `            ``cnt += 1; ` `    ``} ` ` `  `    ``// Prints the last step ` `    ``cout << a[n - 1] << ``" occurs "` `<< cnt << ``" times\n"``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `a[] = { 1, 1, 1, 2, 3, 4, 9, 9, 10 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a[0]); ` ` `  `    ``printElements(a, n); ` `    ``return` `0; ` `} `

 `// Java program to print the elements in ` `// descending along with their frequencies ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to print the elements in descending ` `// along with their frequencies ` `static` `void` `printElements(``int` `a[], ``int` `n) ` `{ ` ` `  `    ``// Sorts the element in decreasing order ` `    ``Arrays.sort(a); ` `    ``a = reverse(a); ` `    ``int` `cnt = ``1``; ` ` `  `    ``// traverse the array elements ` `    ``for` `(``int` `i = ``0``; i < n - ``1``; i++)  ` `    ``{ ` ` `  `        ``// Prints the number and count ` `        ``if` `(a[i] != a[i + ``1``]) ` `        ``{ ` `            ``System.out.print(a[i]+ ``" occurs "` `+ ` `                            ``cnt + ``" times\n"``); ` `            ``cnt = ``1``; ` `        ``} ` `        ``else` `            ``cnt += ``1``; ` `    ``} ` ` `  `    ``// Prints the last step ` `    ``System.out.print(a[n - ``1``]+ ``" occurs "` `+  ` `                    ``cnt + ``" times\n"``); ` `} ` ` `  `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; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `a[] = { ``1``, ``1``, ``1``, ``2``, ``3``, ``4``, ``9``, ``9``, ``10` `}; ` `    ``int` `n = a.length; ` ` `  `    ``printElements(a, n); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

 `# Python3 program to print the elements in  ` `# descending along with their frequencies  ` ` `  `# Function to print the elements in  ` `# descending along with their frequencies  ` `def` `printElements(a, n) : ` ` `  `    ``# Sorts the element in decreasing order  ` `    ``a.sort(reverse ``=` `True``)  ` `    ``cnt ``=` `1` ` `  `    ``# traverse the array elements  ` `    ``for` `i ``in` `range``(n ``-` `1``) : ` ` `  `        ``# Prints the number and count  ` `        ``if` `(a[i] !``=` `a[i ``+` `1``]) :  ` `            ``print``(a[i], ``" occurs "``, cnt, ``"times"``)  ` `            ``cnt ``=` `1` `         `  `        ``else` `: ` `            ``cnt ``+``=` `1` `     `  `    ``# Prints the last step  ` `    ``print``(a[n ``-` `1``], ``"occurs"``, cnt, ``"times"``)  ` ` `  `# Driver Code  ` `if` `__name__ ``=``=` `"__main__"` `: ` ` `  `    ``a ``=` `[ ``1``, ``1``, ``1``, ``2``,  ` `          ``3``, ``4``, ``9``, ``9``, ``10` `]  ` `    ``n ``=` `len``(a) ` ` `  `    ``printElements(a, n) ` `     `  `# This code is contributed by Ryuga `

 `// C# program to print the elements in ` `// descending along with their frequencies ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to print the elements in descending ` `// along with their frequencies ` `static` `void` `printElements(``int` `[]a, ``int` `n) ` `{ ` ` `  `    ``// Sorts the element in decreasing order ` `    ``Array.Sort(a); ` `    ``a = reverse(a); ` `    ``int` `cnt = 1; ` ` `  `    ``// traverse the array elements ` `    ``for` `(``int` `i = 0; i < n - 1; i++)  ` `    ``{ ` ` `  `        ``// Prints the number and count ` `        ``if` `(a[i] != a[i + 1]) ` `        ``{ ` `            ``Console.Write(a[i]+ ``" occurs "` `+ ` `                            ``cnt + ``" times\n"``); ` `            ``cnt = 1; ` `        ``} ` `        ``else` `            ``cnt += 1; ` `    ``} ` ` `  `    ``// Prints the last step ` `    ``Console.Write(a[n - 1]+ ``" occurs "` `+  ` `                    ``cnt + ``" times\n"``); ` `} ` ` `  `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; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `[]a = { 1, 1, 1, 2, 3, 4, 9, 9, 10 }; ` `    ``int` `n = a.Length; ` ` `  `    ``printElements(a, n); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

 ` `

Output:
```10 occurs 1 times
9 occurs 2 times
4 occurs 1 times
3 occurs 1 times
2 occurs 1 times
1 occurs 3 times
```

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Practice Tags :