Sorting element of an array by frequency in decreasing order
Last Updated :
12 Nov, 2023
Given an array arr[] of N integers. The task is to sort the array arr[] according to the frequency of elements in decreasing order.
Note: If the frequencies of the two elements are the same, then the smaller element should come first.
Examples:
Input: arr[] = { 4, 4, 5, 6, 4, 2, 2, 8, 5 }
Output: 4 4 4 2 2 5 5 6 8
Input: arr[] = { 9, 9, 5, 8, 5 }
Output: 5 5 9 9 8
Approach:
- Store the frequency of all elements in array arr[].
- For arr[] = { 4, 4, 5, 6, 4, 2, 2, 8, 5 }
The frequency array of the above array is:
freq[] = { 0, 0, 2, 0, 3, 2, 0, 1, 0, 1}
- Traverse the frequency array and for all the element having frequency greater than 1, update the value in the array arr[] as:
freq[2] = 2
arr[0] = 100000*freq[2] + (100000 – 2) = 299998
freq[4] = 3
arr[1] = 100000*freq[2] + (100000 – 4) = 399996
freq[5] = 2
arr[2] = 100000*freq[2] + (100000 – 5) = 299995
freq[6] = 2
arr[3] = 100000*freq[2] + (100000 – 6) = 199994
freq[8] = 2
arr[4] = 100000*freq[2] + (100000 – 2) = 199994
Now array becomes:
arr[] = {299998, 399996, 299995, 199994, 199992, 2, 2, 8, 5}
- Sort the array arr[] in decreasing order.
- Traverse the array arr[] and to get the element and frequency of that element as per the updation of array element in step 2, do the following:
To get the frequency of current element:
frequency = arr[i]/100000;
To get the value:
value = 100000 – arr[i]%100000
For Example:
if arr[i] = 399996
frequency = arr[i]/100000 = 399996/100000 = 3
value = 100000 – arr[i]%100000 = 100000 – 99996 = 4
The element 4 is having frequency 3.
- For each element in arr[] find the value and frequency(say f) at each index and print the value f number of times.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
int sortByFreq( int * arr, int n)
{
int maxE = -1;
for ( int i = 0; i < n; i++) {
maxE = max(maxE, arr[i]);
}
int freq[maxE + 1] = { 0 };
for ( int i = 0; i < n; i++) {
freq[arr[i]]++;
}
int cnt = 0;
for ( int i = 0; i <= maxE; i++) {
if (freq[i] > 0) {
int value = 100000 - i;
arr[cnt] = 100000 * freq[i] + value;
cnt++;
}
}
return cnt;
}
void printSortedArray( int * arr, int cnt)
{
for ( int i = 0; i < cnt; i++) {
int frequency = arr[i] / 100000;
int value = 100000 - (arr[i] % 100000);
for ( int j = 0; j < frequency; j++) {
cout << value << ' ' ;
}
}
}
int main()
{
int arr[] = { 4, 4, 5, 6, 4, 2, 2, 8, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
int cnt = sortByFreq(arr, n);
sort(arr, arr + cnt, greater< int >());
printSortedArray(arr, cnt);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int sortByFreq(Integer []arr, int n)
{
int maxE = - 1 ;
for ( int i = 0 ; i < n; i++) {
maxE = Math.max(maxE, arr[i]);
}
int freq[] = new int [maxE + 1 ];
for ( int i = 0 ; i < n; i++) {
freq[arr[i]]++;
}
int cnt = 0 ;
for ( int i = 0 ; i <= maxE; i++) {
if (freq[i] > 0 ) {
int value = 100000 - i;
arr[cnt] = 100000 * freq[i] + value;
cnt++;
}
}
return cnt;
}
static void printSortedArray(Integer []arr, int cnt)
{
for ( int i = 0 ; i < cnt; i++) {
int frequency = arr[i] / 100000 ;
int value = 100000 - (arr[i] % 100000 );
for ( int j = 0 ; j < frequency; j++) {
System.out.print(value + " " );
}
}
}
public static void main(String[] args)
{
Integer arr[] = { 4 , 4 , 5 , 6 , 4 , 2 , 2 , 8 , 5 };
int n = arr.length;
int cnt = sortByFreq(arr, n);
Arrays.sort(arr, Collections.reverseOrder());
printSortedArray(arr, cnt);
}
}
|
Python3
def sortByFreq(arr, n):
maxE = - 1 ;
for i in range (n):
maxE = max (maxE, arr[i])
freq = [ 0 ] * (maxE + 1 );
for i in range (n):
freq[arr[i]] + = 1 ;
cnt = 0 ;
for i in range (maxE + 1 ):
if (freq[i] > 0 ):
value = 100000 - i;
arr[cnt] = 100000 * freq[i] + value;
cnt + = 1 ;
return cnt;
def printSortedArray(arr, cnt):
for i in range (cnt):
frequency = arr[i] / 100000 ;
value = 100000 - (arr[i] % 100000 );
for j in range ( int (frequency)):
print (value, end = " " )
if __name__ = = '__main__' :
arr = [ 4 , 4 , 5 , 6 , 4 , 2 , 2 , 8 , 5 ]
n = len (arr)
cnt = sortByFreq(arr, n);
arr.sort(reverse = True )
printSortedArray(arr, cnt);
|
C#
using System;
class GFG {
static int sortByFreq( int [] arr, int n)
{
int maxE = -1;
for ( int i = 0; i < n; i++)
{
maxE = Math.Max(maxE, arr[i]);
}
int [] freq = new int [maxE + 1];
for ( int i = 0; i < n; i++)
{
freq[arr[i]]++;
}
int cnt = 0;
for ( int i = 0; i <= maxE; i++)
{
if (freq[i] > 0)
{
int value = 100000 - i;
arr[cnt] = 100000 * freq[i] + value;
cnt++;
}
}
return cnt;
}
static void printSortedArray( int [] arr, int cnt)
{
for ( int i = 0; i < cnt; i++)
{
int frequency = arr[i] / 100000;
int value = 100000 - (arr[i] % 100000);
for ( int j = 0; j < frequency; j++)
{
Console.Write(value + " " );
}
}
}
public static void Main()
{
int [] arr = { 4, 4, 5, 6, 4, 2, 2, 8, 5 };
int n = arr.Length;
int cnt = sortByFreq(arr, n);
Array.Sort(arr);
Array.Reverse(arr);
printSortedArray(arr, cnt);
}
}
|
Javascript
<script>
function sortByFreq(arr, n) {
var maxE = -1;
for ( var i = 0; i < n; i++) {
maxE = Math.max(maxE, arr[i]);
}
var freq = new Array(maxE + 1).fill(0);
for ( var i = 0; i < n; i++) {
freq[arr[i]]++;
}
var cnt = 0;
for ( var i = 0; i <= maxE; i++) {
if (freq[i] > 0) {
var value = 100000 - i;
arr[cnt] = 100000 * freq[i] + value;
cnt++;
}
}
return cnt;
}
function printSortedArray(arr, cnt) {
for ( var i = 0; i < cnt; i++) {
var frequency = parseInt(arr[i] / 100000);
var value = 100000 - (arr[i] % 100000);
for ( var j = 0; j < frequency; j++) {
document.write(value + " " );
}
}
}
var arr = [4, 4, 5, 6, 4, 2, 2, 8, 5];
var n = arr.length;
var cnt = sortByFreq(arr, n);
arr.sort((a, b) => b - a);
printSortedArray(arr, cnt);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
STL Pair and Comparator based approach :
Approach:
1. Store the frequency of each element in a map.
2. Iterate the map and store the each element and it’s frequency in a vector of pairs.
3. Pass a comparator which sorts the elements in decreasing order of their frequency and by elements value if frequency is equal.
4.Push the elements their frequency number of times inside the final array.
C++
#include<bits/stdc++.h>
using namespace std;
bool compare(pair< int , int >p1,pair< int , int >p2)
{
if (p1.second==p2.second)
{
return p1.first<p2.first;
}
return p1.second>p2.second;
}
int main()
{
vector< int >arr={4,4,5,6,4,2,2,8,5};
int n=arr.size();
map< int , int >m;
for ( int i=0;i<n;i++)
{
m[arr[i]]+=1;
}
vector<pair< int , int >>a;
for ( auto it=m.begin();it!=m.end();it++)
{
a.push_back(make_pair(it->first,it->second));
}
sort(a.begin(),a.end(),compare);
vector< int >ans;
for ( auto x:a)
{
while (x.second--)
{
ans.push_back(x.first);
}
}
for ( auto x:ans)
{
cout<<x<< " " ;
}
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int compare( int [] p1, int [] p2) {
if (p1[ 1 ] == p2[ 1 ])
{
return p1[ 0 ] - p2[ 0 ];
}
return p2[ 1 ] - p1[ 1 ];
}
public static void main(String[] args) {
int [] arr = { 4 , 4 , 5 , 6 , 4 , 2 , 2 , 8 , 5 };
int n = arr.length;
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
for ( int i = 0 ; i < n; i++) {
if (m.containsKey(arr[i])) {
m.put(arr[i], m.get(arr[i]) + 1 );
} else
m.put(arr[i], 1 );
}
int [][] a = new int [m.size()][ 2 ];
int i = 0 ;
for (Map.Entry<Integer, Integer> x : m.entrySet()) {
a[i][ 0 ] = x.getKey();
a[i][ 1 ] = x.getValue();
i++;
}
Arrays.sort(a, Main::compare);
int [] ans = new int [n];
int j = 0 ;
for ( int [] x : a) {
while (x[ 1 ]-- > 0 ) {
ans[j++] = x[ 0 ];
}
}
for ( int x : ans) {
System.out.print(x + " " );
}
}
}
|
Python3
from functools import cmp_to_key
def compare(p1, p2):
if (p1[ 1 ] = = p2[ 1 ]):
return p1[ 0 ] - p2[ 0 ]
return p2[ 1 ] - p1[ 1 ]
arr = [ 4 , 4 , 5 , 6 , 4 , 2 , 2 , 8 , 5 ]
n = len (arr)
m = {}
for i in range (n):
if (arr[i] in m):
m[arr[i]] = m[arr[i]] + 1
else :
m[arr[i]] = 1
a = []
for x,y in m.items():
a.append([x,y])
a.sort(key = cmp_to_key(compare))
ans = []
for x in a:
while (x[ 1 ]):
ans.append(x[ 0 ])
x[ 1 ] - = 1
for x in ans:
print (x, end = " " )
|
C#
using System;
using System.Collections.Generic;
class Program {
static int Compare(KeyValuePair< int , int > p1, KeyValuePair< int , int > p2) {
if (p1.Value == p2.Value) {
return p1.Key.CompareTo(p2.Key);
}
return p2.Value.CompareTo(p1.Value);
}
static void Main( string [] args) {
List< int > arr = new List< int >{4, 4, 5, 6, 4, 2, 2, 8, 5};
Dictionary< int , int > m = new Dictionary< int , int >();
for ( int i = 0; i < arr.Count; i++) {
if (!m.ContainsKey(arr[i])) {
m[arr[i]] = 0;
}
m[arr[i]]++;
}
List<KeyValuePair< int , int >> a = new List<KeyValuePair< int , int >>();
foreach (KeyValuePair< int , int > entry in m) {
a.Add(entry);
}
a.Sort(Compare);
List< int > ans = new List< int >();
foreach (KeyValuePair< int , int > x in a) {
for ( int i = 0; i < x.Value; i++) {
ans.Add(x.Key);
}
}
foreach ( int x in ans) {
Console.Write(x + " " );
}
}
}
|
Javascript
<script>
function compare(p1, p2)
{
if (p1[1] == p2[1])
{
return p1[0] - p2[0];
}
return p2[1] - p1[1];
}
let arr = [4,4,5,6,4,2,2,8,5];
let n = arr.length;
let m = new Map();
for (let i = 0; i < n; i++)
{
if (m.has(arr[i])){
m.set(arr[i], m.get(arr[i]) + 1);
}
else m.set(arr[i],1);
}
let a = [];
for (let [x,y] of m)
{
a.push([x,y]);
}
a.sort(compare);
let ans = [];
for (let x of a){
while (x[1]--){
ans.push(x[0]);
}
}
for (let x of ans)
{
document.write(x, " " );
}
</script>
|
Time Complexity: O(N*logN) as sort() takes N*logN time (Here N is size of input array). Also, insertion in ordered map takes logN time and since N insertions are done so it also contributes N*logN time. So overall complexity will be O(N*logN).
Using priority_queue:
This approach is based on the idea to use priority_queue with pair of elements as its entry element. The first element of the pair will be frequency of element of the array and second one will be the array element itself. By inserting elements this way in the priority_queue, we will be having highest frequency element at top. So when we pop the priority_queue, we will be having the second entry of the pair which was at top of the priority_queue as our element of the result array. To store the frequency we will be using map as this frequency will be inserted along with element in the priority_queue.
We will be using custom comparator for the priority_queue which sorts the elements in decreasing order of their frequency and by elements value if frequency is equal.
Algorithm:
- Create a class named ‘Compare’ to define a custom comparison operator for a priority queue of pairs of integers.
- In the ‘Compare’ class, define the comparison operator such that pairs are sorted first by their first element in descending order, and then by their second element in ascending order.
- Create a map to store the frequency of each element in the array.
- Traverse through the array and increment the frequency of each element in the map.
- Create a priority queue to store pairs of elements and their frequency in decreasing order.
- Traverse through the map and insert each element and its frequency into the priority queue.
- Declare a new array to store the sorted elements.
- Traverse through the priority queue and insert the elements into the new array based on their frequency.
- Return the resultant array res.
- Output the sorted array.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Compare {
public :
bool operator()(pair< int , int > p1, pair< int , int > p2)
{
if (p1.first == p2.first) {
return p1.second > p2.second;
}
return p1.first < p2.first;
}
};
vector< int > sortByFrequency( int arr[], int n) {
map< int , int > freqMap;
for ( int i = 0; i < n; i++) {
freqMap[arr[i]]++;
}
priority_queue<pair< int , int >, vector<pair< int , int >>, Compare > pq;
for ( auto it = freqMap.begin(); it != freqMap.end();
it++) {
pq.push(make_pair(it->second, it->first));
}
vector< int > res;
int index = 0;
while (!pq.empty()) {
int freq = pq.top().first;
int element = pq.top().second;
pq.pop();
for ( int i = 0; i < freq; i++) {
res.push_back( element );
}
}
return res;
}
int main() {
int arr[] = { 4, 4, 5, 6, 4, 2, 2, 8, 5 };
int n = sizeof (arr) / sizeof ( int );
vector< int > ans = sortByFrequency(arr, n);
for ( int i = 0; i < n; i++) {
cout << ans[i] << " " ;
}
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
int [] arr = { 4 , 4 , 5 , 6 , 4 , 2 , 2 , 8 , 5 };
List<Integer> ans = sortByFrequency(arr);
for ( int element : ans) {
System.out.print(element + " " );
}
}
static List<Integer> sortByFrequency( int [] arr) {
Map<Integer, Integer> freqMap = new HashMap<>();
for ( int element : arr) {
freqMap.put(element, freqMap.getOrDefault(element, 0 ) + 1 );
}
List<Map.Entry<Integer, Integer>> sortedElements = new ArrayList<>(freqMap.entrySet());
sortedElements.sort((a, b) -> {
int freqComparison = Integer.compare(b.getValue(), a.getValue());
return (freqComparison == 0 ) ? Integer.compare(a.getKey(), b.getKey()) : freqComparison;
});
List<Integer> res = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : sortedElements) {
int element = entry.getKey();
int freq = entry.getValue();
for ( int i = 0 ; i < freq; i++) {
res.add(element);
}
}
return res;
}
}
|
Python3
from collections import Counter
def sort_by_frequency(arr):
freq_map = Counter(arr)
sorted_elements = sorted (freq_map.items(), key = lambda x: ( - x[ 1 ], x[ 0 ]))
res = []
for element, freq in sorted_elements:
res.extend([element] * freq)
return res
if __name__ = = "__main__" :
arr = [ 4 , 4 , 5 , 6 , 4 , 2 , 2 , 8 , 5 ]
ans = sort_by_frequency(arr)
for element in ans:
print (element, end = " " )
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static List< int > SortByFrequency(List< int > arr)
{
Dictionary< int , int > freqMap = new Dictionary< int , int >();
foreach ( int element in arr)
{
if (freqMap.ContainsKey(element))
{
freqMap[element]++;
}
else
{
freqMap[element] = 1;
}
}
var sortedElements = freqMap.OrderByDescending(x => x.Value).ThenBy(x => x.Key);
List< int > res = new List< int >();
foreach ( var kvp in sortedElements)
{
int element = kvp.Key;
int freq = kvp.Value;
res.AddRange(Enumerable.Repeat(element, freq));
}
return res;
}
static void Main( string [] args)
{
List< int > arr = new List< int > { 4, 4, 5, 6, 4, 2, 2, 8, 5 };
List< int > ans = SortByFrequency(arr);
foreach ( int element in ans)
{
Console.Write(element + " " );
}
}
}
|
Javascript
function sortByFrequency(arr) {
const freqMap = new Map();
for (const element of arr) {
freqMap.set(element, (freqMap.get(element) || 0) + 1);
}
const sortedElements = [...freqMap.entries()].sort((a, b) => {
const freqComparison = b[1] - a[1];
return freqComparison === 0 ? a[0] - b[0] : freqComparison;
});
const res = [];
for (const [element, freq] of sortedElements) {
for (let i = 0; i < freq; i++) {
res.push(element);
}
}
return res;
}
const arr = [4, 4, 5, 6, 4, 2, 2, 8, 5];
const ans = sortByFrequency(arr);
console.log(ans.join( ' ' ));
|
Time Complexity: O(n * log2n) where n is the size of input array. This is because priority_queue insertion takes (n * log2n) time as n elements are inserted.
Space Complexity: O(n) where n is size of input array. This is because freqMap map and res vector has been created.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...