Print numbers in descending order along with their frequencies
Last Updated :
06 Apr, 2023
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
Naive approach: Use some Data-Structure (e.g. multiset) that stores elements in decreasing order and then print the elements one by one with its 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:
CPP
#include <bits/stdc++.h>
using namespace std;
void printElements(int a[], int n)
{
multiset<int, greater<int> > ms;
for (int i = 0; i < n; i++) {
ms.insert(a[i]);
}
while (!ms.empty()) {
int maxel = *ms.begin();
int times = ms.count(maxel);
cout << maxel << " occurs " << times << " times\n";
ms.erase(maxel);
}
}
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;
}
|
Python3
def printElements(a):
ms = sorted(a, reverse=True)
while ms:
maxel = ms[0]
times = ms.count(maxel)
print(f"{maxel} occurs {times} times")
ms = [x for x in ms if x != maxel]
if __name__ == '__main__':
a = [1, 1, 1, 2, 3, 4, 9, 9, 10]
printElements(a)
|
Javascript
function printElements(a) {
const ms = new Map();
for (let i = 0; i < a.length; i++) {
const key = a[i];
ms.set(key, (ms.get(key) || 0) + 1);
}
const elements = [];
for (const [key, value] of ms.entries()) {
elements.push(`${key} occurs ${value} times`);
}
for (let i = elements.length - 1; i >= 0; i--) {
console.log(elements[i]);
}
}
const a = [1, 1, 1, 2, 3, 4, 9, 9, 10];
printElements(a);
|
Java
import java.util.*;
public class Main {
static void printElements(int[] a, int n) {
TreeMap<Integer, Integer> tm = new TreeMap<>(Collections.reverseOrder());
for (int i = 0; i < n; i++) {
if (tm.containsKey(a[i])) {
tm.put(a[i], tm.get(a[i]) + 1);
} else {
tm.put(a[i], 1);
}
}
for (Map.Entry<Integer, Integer> entry : tm.entrySet()) {
int element = entry.getKey();
int frequency = entry.getValue();
System.out.println(element + " occurs " + frequency + " times");
}
}
public static void main(String[] args) {
int[] a = { 1, 1, 1, 2, 3, 4, 9, 9, 10 };
int n = a.length;
printElements(a, n);
}
}
|
C#
using System;
using System.Collections.Generic;
public class Program {
static void PrintElements(int[] a, int n) {
SortedDictionary<int, int> sd = new SortedDictionary<int, int>(new DescendingComparer());
for (int i = 0; i < n; i++) {
if (sd.ContainsKey(a[i])) {
sd[a[i]]++;
} else {
sd[a[i]] = 1;
}
}
foreach (KeyValuePair<int, int> entry in sd) {
int element = entry.Key;
int frequency = entry.Value;
Console.WriteLine(element + " occurs " + frequency + " times");
}
}
class DescendingComparer : IComparer<int> {
public int Compare(int x, int y) {
return y.CompareTo(x);
}
}
public static void Main() {
int[] a = { 1, 1, 1, 2, 3, 4, 9, 9, 10 };
int n = a.Length;
PrintElements(a, n);
}
}
|
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
Time Complexity: O(N*logN), as we are using a loop to traverse N times and in each traversal, we are doing a multiset operation which will cost us logN time.
Auxiliary Space: O(N), as we are using extra space for the multiset.
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++
#include <bits/stdc++.h>
using namespace std;
void printElements(int a[], int n)
{
sort(a, a + n, greater<int>());
int cnt = 1;
for (int i = 0; i < n - 1; i++) {
if (a[i] != a[i + 1]) {
cout << a[i] << " occurs " << cnt << " times\n";
cnt = 1;
}
else
cnt += 1;
}
cout << a[n - 1] << " occurs " << cnt << " times\n";
}
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
import java.util.*;
class GFG
{
static void printElements(int a[], int n)
{
Arrays.sort(a);
a = reverse(a);
int cnt = 1;
for (int i = 0; i < n - 1; i++)
{
if (a[i] != a[i + 1])
{
System.out.print(a[i]+ " occurs " +
cnt + " times\n");
cnt = 1;
}
else
cnt += 1;
}
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;
}
public static void main(String[] args)
{
int a[] = { 1, 1, 1, 2, 3, 4, 9, 9, 10 };
int n = a.length;
printElements(a, n);
}
}
|
Python3
def printElements(a, n) :
a.sort(reverse = True)
cnt = 1
for i in range(n - 1) :
if (a[i] != a[i + 1]) :
print(a[i], " occurs ", cnt, "times")
cnt = 1
else :
cnt += 1
print(a[n - 1], "occurs", cnt, "times")
if __name__ == "__main__" :
a = [ 1, 1, 1, 2,
3, 4, 9, 9, 10 ]
n = len(a)
printElements(a, n)
|
C#
using System;
class GFG
{
static void printElements(int []a, int n)
{
Array.Sort(a);
a = reverse(a);
int cnt = 1;
for (int i = 0; i < n - 1; i++)
{
if (a[i] != a[i + 1])
{
Console.Write(a[i]+ " occurs " +
cnt + " times\n");
cnt = 1;
}
else
cnt += 1;
}
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;
}
public static void Main(String[] args)
{
int []a = { 1, 1, 1, 2, 3, 4, 9, 9, 10 };
int n = a.Length;
printElements(a, n);
}
}
|
PHP
<?php
function printElements(&$a, $n)
{
rsort($a);
$cnt = 1;
for ($i = 0; $i < $n - 1; $i++)
{
if ($a[$i] != $a[$i + 1])
{
echo ($a[$i]);
echo (" occurs " );
echo $cnt ;
echo (" times\n");
$cnt = 1;
}
else
$cnt += 1;
}
echo ($a[$n - 1]);
echo (" occurs ");
echo $cnt;
echo (" times\n");
}
$a = array(1, 1, 1, 2, 3,
4, 9, 9, 10 );
$n = sizeof($a);
printElements($a, $n);
?>
|
Javascript
<script>
function printElements(a, n)
{
a=a.sort(compare);
a = reverse(a);
var cnt = 1;
for (var i = 0; i < n - 1; i++)
{
if (a[i] != a[i + 1])
{
document.write(a[i]+ " occurs " + cnt + " times" + "<br>");
cnt = 1;
}
else
cnt += 1;
}
document.write(a[n - 1]+ " occurs " + cnt + " times" + "<br>");
}
function reverse(a){
var 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;
}
function compare(a, b) {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
}
var a = [ 1, 1, 1, 2, 3, 4, 9, 9, 10 ];
var n = a.length;
printElements(a, n);
</script>
|
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
Time Complexity: O(N*logN), as we are using the sort function which will cost us O(N*logN) time.
Auxiliary Space: O(1), as we are not using any extra space.
Please Login to comment...