Sort elements by frequency | Set 4 (Efficient approach using hash)

Print the elements of an array in the decreasing frequency if 2 numbers have same frequency then print the one which came first.

Examples:

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

Input : arr[] = {2, 5, 2, 6, -1, 9999999, 5, 8, 8, 8}
Output : arr[] = {8, 8, 8, 2, 2, 5, 5, 6, -1, 9999999}

We have discussed different approaches in below posts :
Sort elements by frequency | Set 1
Sort elements by frequency | Set 2
Sorting Array Elements By Frequency | Set 3 (Using STL)

All of the above approaches work in O(n Log n) time where n is total number of elements. In this post, a new approach is discussed that works in O(n + m Log m) time where n is total number of elements and m is total number of distinct elements.

The idea is to use hashing.

  1. We insert all elements and their counts into a hash. This step takes O(n) time where n is number of elements.
  2. We copy contents of hash to an array (or vector) and sort them by counts. This step takes O(m Log m) time where m is total number of distinct elements.
  3. For maintaining the order of elements if the frequency is same, we use another hash which has the key as elements of the array and value as the index. If the frequency is same for two elements then sort elements according to the index.

Below image is a dry run of the above approach:

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Sort elements by frequency. If two elements have same
// count, then put the elements that appears first
#include <bits/stdc++.h>
using namespace std;
  
// Map m2 keeps track of indexes of elements in array
unordered_map<int, int> m2;
  
// Used for sorting by frequency. And if frequency is same,
// then by appearance
bool sortByVal(const pair<int, int>& a, const pair<int, int>& b)
{
    // If frequency is same then sort by index
    if (a.second == b.second) 
        return m2[a.first] < m2[b.first];
      
    return a.second > b.second;
}
  
// function to sort elements by frequency
void sortByFreq(int a[], int n)
{
    unordered_map<int, int> m;
    vector<pair<int, int> > v;
  
    for (int i = 0; i < n; ++i) {
  
        // Map m is used to keep track of count 
        // of elements in array
        m[a[i]]++;
  
        // Update the value of map m2 only once
        if (m2[a[i]] == 0) 
            m2[a[i]] = i + 1;        
    }
  
    // Copy map to vector
    copy(m.begin(), m.end(), back_inserter(v));
  
    // Sort the element of array by frequency
    sort(v.begin(), v.end(), sortByVal);
  
    for (int i = 0; i < v.size(); ++i) 
        for (int j = 0; j < v[i].second; ++j) 
            cout << v[i].first << " "
}
  
// Driver program
int main()
{
    int a[] = { 2, 5, 2, 6, -1, 9999999, 5, 8, 8, 8 };
    int n = sizeof(a) / sizeof(a[0]);
  
    sortByFreq(a, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.io.IOException;
import java.util.*;
  
class GFG {
    public static StringBuffer sortByFrequency(int arr1[], int l1) {
        // Build a map of array elements to its count
        Map<Integer, Integer> countMap = getCountMap(arr1, l1);
        StringBuffer result = new StringBuffer();
          
        // Sort the map using a comparingByValue comparator
        // build the result by appending keys the count times to the result
          
        countMap.entrySet().stream()
                .sorted(Map.Entry.<Integer, Integer> comparingByValue().reversed())
                .forEach(e -> {
                    int key = e.getKey();
                    int val = e.getValue();
                    for (int i = 0; i < val; i++) {
                        result.append(key + " ");
                    }
                });
          
        return result;
    }
  
    // helper function to return the element count map
    private static Map<Integer, Integer> getCountMap(int[] arr, int l1) {
        Map<Integer, Integer> countMap = new LinkedHashMap<>();
        for (int i = 0; i < l1; i++) {
            if (countMap.containsKey(arr[i])) {
                countMap.put(arr[i], countMap.get(arr[i]) + 1);
            } else {
                countMap.put(arr[i], 1);
            }
        }
        return countMap;
    }
      
    // Driver program
    public static void main(String[] args) throws IOException {
        int a[] = { 2, 5, 2, 6, -1, 9999999, 5, 8, 8, 8 };
        System.out.println(sortByFrequency(a, a.length));
    }
}

chevron_right


Output:

8 8 8 2 2 5 5 6 -1 9999999 

Time Complexity : O(n) + O(m Log m) where n is total number of elements and m is total number of distinct elements

This article is contributed by Ankur Singh and improved by Ankur Goel. 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.



My Personal Notes arrow_drop_up

Improved By : AnkurGoel