Sort an array according to the increasing frequency of the digit K in the array elements
Last Updated :
25 Mar, 2022
Given an array arr[] of size N, and an integer K representing a digit, the task is to print the given array in increasing order according to the increasing frequency of the digit K in the array elements.
Examples:
Input: arr[] = {15, 66, 26, 91}, K = 6
Output: 15 91 26 66
Explanation:
The frequency of digit 6 in array elements is {0, 2, 1, 0}. The elements in increasing order of frequency of the digit 6 in {15, 91, 26, 66}.
Input: arr[] = {20, 21, 0}, K = 0
Output: 21 20 0
Explanation:
The frequency of digit 0 in array elements is {1, 0, 1}. The elements in increasing order of frequency of the digit 0 is {21, 20, 0}.
Approach: The idea is to count the occurrences of the digit K for each element of the array and sort the array according to it. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countOccurrences( int num, int K)
{
if (K == 0 && num == 0)
return 1;
int count = 0;
while (num > 0) {
if (num % 10 == K)
count++;
num /= 10;
}
return count;
}
void sortOccurrences( int arr[],
int N, int K)
{
multimap< int , int > mp;
for ( int i = 0; i < N; i++) {
int count = countOccurrences(
arr[i], K);
mp.insert(pair< int , int >(
count, arr[i]));
}
for ( auto & itr : mp) {
cout << itr.second << " " ;
}
}
int main()
{
int arr[] = { 15, 66, 26, 91 };
int K = 6;
int N = sizeof (arr) / sizeof (arr[0]);
sortOccurrences(arr, N, K);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static int countOccurrences( int num, int K)
{
if (K == 0 && num == 0 )
return 1 ;
int count = 0 ;
while (num > 0 ) {
if (num % 10 == K)
count++;
num /= 10 ;
}
return count;
}
static class Pair {
int idx;
int freq;
Pair( int idx, int freq)
{
this .idx = idx;
this .freq = freq;
}
}
static void sortOccurrences( int arr[], int N, int K)
{
Pair mp[] = new Pair[N];
for ( int i = 0 ; i < N; i++) {
int count = countOccurrences(arr[i], K);
mp[i] = new Pair(i, count);
}
Arrays.sort(mp, (p1, p2) -> {
if (p1.freq == p2.freq)
return p1.idx - p2.idx;
return p1.freq - p2.freq;
});
for (Pair p : mp) {
System.out.print(arr[p.idx] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 15 , 66 , 26 , 91 };
int K = 6 ;
int N = arr.length;
sortOccurrences(arr, N, K);
}
}
|
Python3
def countOccurrences( num, K):
if (K = = 0 and num = = 0 ):
return 1
count = 0
while (num > 0 ):
if (num % 10 = = K):
count + = 1
num / / = 10
return count
def sortOccurrences(arr, N, K):
mp = []
for i in range (N):
count = countOccurrences(arr[i], K)
mp.append([count, arr[i]])
mp = sorted (mp)
for itr in mp:
print (itr[ 1 ], end = ' ' )
arr = [ 15 , 66 , 26 , 91 ]
K = 6
N = len (arr)
sortOccurrences(arr, N, K);
|
C#
using System;
public class GFG
{
static int countOccurrences( int num, int K)
{
if (K == 0 && num == 0)
return 1;
int count = 0;
while (num > 0) {
if (num % 10 == K)
count++;
num /= 10;
}
return count;
}
class Pair : IComparable<Pair> {
public int idx;
public int freq;
public Pair( int idx, int freq)
{
this .idx = idx;
this .freq = freq;
}
public int CompareTo(Pair p)
{
if ( this .freq == p.freq)
return this .idx - p.idx;
return this .freq - p.freq;
}
}
static void sortOccurrences( int []arr, int N, int K)
{
Pair []mp = new Pair[N];
for ( int i = 0; i < N; i++) {
int count = countOccurrences(arr[i], K);
mp[i] = new Pair(i, count);
}
Array.Sort(mp);
foreach (Pair p in mp) {
Console.Write(arr[p.idx] + " " );
}
}
public static void Main(String[] args)
{
int []arr = { 15, 66, 26, 91 };
int K = 6;
int N = arr.Length;
sortOccurrences(arr, N, K);
}
}
|
Javascript
<script>
function countOccurrences( num, K){
if (K == 0 && num == 0)
return 1
count = 0
while (num > 0){
if (num % 10 == K)
count += 1
num = Math.floor(num/10)
}
return count
}
function sortOccurrences(arr, N, K){
mp = []
for (let i=0;i<N;i++){
let count = countOccurrences(arr[i], K)
mp.push([count, arr[i]])
}
mp.sort()
for ([itr1,itr2] of mp)
document.write(itr2, ' ' )
}
let arr = [ 15, 66, 26, 91 ]
let K = 6
let N = arr.length
sortOccurrences(arr, N, K)
</script>
|
Time Complexity: O(N*log10N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...