VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sort-elements-by-frequency/

⇱ Sort elements by frequency - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sort elements by frequency

Last Updated : 23 Jul, 2025

Given an array of integers arr[], sort the array according to the frequency of elements, i.e. elements that have higher frequency comes first. If the frequencies of two elements are the same, then the smaller number comes first.

Examples:

Input: arr[] = [5, 5, 4, 6, 4]
Output: [4, 4, 5, 5, 6]
Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are the same the smaller element comes first. So 4 comes first then comes 5. Finally comes 6. The output is 4 4 5 5 6.

Input: arr[] = [9, 9, 9, 2, 5]
Output: [9, 9, 9, 2, 5]
Explanation: The highest frequency here is 3. Element 9 has the highest frequency So 9 comes first. Now both 2 and 5 have the same frequency. So we print smaller elements first. The output is 9 9 9 2 5.

Using Sorting - O(n * log n) Time and O(n) Space

The idea is to use sorting to arrange the similar elements together, the count frequencies using linear traversal. Store frequencies and items in a 2d array of elements. Finally sort this 2d array according to the frequency of each element.

Illustration

Input: arr[] = {2 5 2 8 5 6 8 8}

Step1: Sort the array, 
After sorting we get: 2 2 5 5 6 8 8 8

Step 2: Now construct the 2D array to maintain the count of every element as {freq, element}
{{2, 2}, {2, 5}, {1, 6}, {3, 8}}

Step 3: Sort the array by count
{{3, 8}, {2, 2}, {2, 5}, {1, 6}}

Set 4: Construct the result array by taking taking the second element and its count as first element
ans[] = {8, 8, 8, 2, 2, 5, 5, 6}


Output
4 4 5 5 6 

Using Hashing and Sorting - O(n * log n) Time and O(n) Space

In the above approach we are firstly sorting the given array to create the frequency array, but instead of doing so, we can use Hash Map or Dictionary. The idea is to store the count of each element in a Hash Map, and then create the frequency array similar to above approach.


Output
4 4 5 5 6 

Using Binary Search Tree and Sorting - O(n * log n) Time and O(n) Space

The idea is to store the elements in the form of BST, and if an element in already present then increment the count of the corresponding node. Thereafter store the element and its frequency in a 2d array and sort it based on the frequency. This approach has been discussed in article Sort elements by frequency using BST

Using Hash Map and Heap - O(n * log n) Time and O(n) Space

The idea is to firstly create the value - frequency table using the Hash Map, then make a Heap such that high frequency remains at top.

Follow the given steps to solve the problem:

  • Take the array and use Hash Map to create value - frequency table
  • Then make a heap such that high frequency remains at top and when frequency is same, just keep in ascending order.
  • Store the negative of element in a heap, to make sure that the elements with same frequency are sorted in ascending order.
  • Then after full insertion into Heap, pop one by one and store it into the array.

Below is the implementation of the above approach:


Output
4 4 5 5 6 
Comment
Article Tags: