# Sorting Array Elements By Frequency | Set 3 (Using STL)

Given an array of integers, sort the array according to frequency of elements. If frequencies of two elements are same, print them in increasing order.

Examples:

Input : arr[] = {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12} Output : 3 3 3 3 2 2 2 12 12 4 5 Explanation : No. Freq 2 : 3 3 : 4 4 : 1 5 : 1 12 : 2

We have discussed different approaches in below posts :

Sort elements by frequency | Set 1

Sort elements by frequency | Set 2

We can solve this problem using map and pairs. Initially we create a map such that map[element] = freq. Once we are done building the map, we create an array of pairs. A pair which stores elements and their corresponding frequency will be used for the purpose of sorting. We write a custom compare function which compares two pairs firstly on the basis of freq and if there is a tie on the basis of values.

Below is its c++ implementation :

`// C++ program to sort elements by frequency using ` `// STL ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to compare two pairs for inbuilt sort ` `bool` `compare(pair<` `int` `,` `int` `> &p1, ` ` ` `pair<` `int` `, ` `int` `> &p2) ` `{ ` ` ` `// If frequencies are same, compare ` ` ` `// values ` ` ` `if` `(p1.second == p2.second) ` ` ` `return` `p1.first < p2.first; ` ` ` `return` `p1.second > p2.second; ` `} ` ` ` `// function to print elements sorted by freq ` `void` `printSorted(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Store items and their frequencies ` ` ` `map<` `int` `, ` `int` `> m; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `m[arr[i]]++; ` ` ` ` ` `// no of distinct values in the array ` ` ` `// is equal to size of map. ` ` ` `int` `s = m.size(); ` ` ` ` ` `// an array of pairs ` ` ` `pair<` `int` `, ` `int` `> p[s]; ` ` ` ` ` `// Fill (val, freq) pairs in an array ` ` ` `// of pairs. ` ` ` `int` `i = 0; ` ` ` `for` `(` `auto` `it = m.begin(); it != m.end(); ++it) ` ` ` `p[i++] = make_pair(it->first, it->second); ` ` ` ` ` `// sort the array of pairs using above ` ` ` `// compare function. ` ` ` `sort(p, p + s, compare); ` ` ` ` ` `cout << ` `"Elements sorted by frequency are: "` `; ` ` ` `for` `(` `int` `i = 0; i < s; i++) ` ` ` `{ ` ` ` `int` `freq = p[i].second; ` ` ` `while` `(freq--) ` ` ` `cout << p[i].first << ` `" "` `; ` ` ` `} ` `} ` ` ` `// driver program ` `int` `main() ` `{ ` ` ` `int` `arr[] = {2, 3, 2, 4, 5, 12, 2, 3, ` ` ` `3, 3, 12}; ` ` ` `int` `n = ` `sizeof` `(arr)/ ` `sizeof` `(arr[0]); ` ` ` `printSorted(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

Elements sorted by frequency are: 3 3 3 3 2 2 2 12 12 4 5

Time Complexity : O(n Log n)

This article is contributed by **Aditi Sharma**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Sum of all odd frequency elements in an array
- Sorting all array elements except one
- Sum of elements in an array having prime frequency
- XOR of elements in an array having prime frequency
- Sorting array except elements in a subarray
- Find the element having different frequency than other array elements
- Array range queries for elements with frequency same as value
- Product of elements in an array having prime frequency
- Replace every elements in the array by its frequency in the array
- Sorting array elements with set bits equal to K
- Fill an array based on frequency where elements are in range from 0 to n-1
- Remove elements from the array whose frequency lies in the range [l, r]
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Sort elements by frequency | Set 1