Find the array element having maximum frequency of the digit K
Last Updated :
19 Sep, 2023
Given an array arr[] of size N and an integer K, the task is to find an array element that contains the digit K a maximum number of times. If more than one solutions exist, then print any one of them. Otherwise, print -1.
Examples:
Input: arr[] = {3, 77, 343, 456}, K = 3
Output: 343
Explanation:
Frequency of 3 in array elements: 1, 0, 2, 0
343 has maximum frequency i.e. 2.
Input: arr[] = {1, 1111, 111, 11}, K = 1
Output: 1111
Explanation:
Frequency of 1 in array elements: 1, 4, 3, 2
1111 has maximum frequency i.e. 4.
Approach: The idea is to traverse the array and for every individual array element, count the occurrences of the digit K in it. Follow the steps below to solve the problem:
- Initialize the max frequency of digit K, say maxfreq, as 0.
- Traverse the given array from the start element till the end.
- For every traversed element, find the frequency of digit K in that element. If it is greater than maxfreq, then update maxfreq and store that element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countFreq( int N, int K)
{
int count = 0;
while (N > 0) {
if (N % 10 == K) {
count++;
}
N = N / 10;
}
return count;
}
int findElementUtil( int arr[], int N, int K)
{
int c = 0;
int max = 0;
int ele;
max = 0;
for ( int i = 0; i < N; i++) {
c = countFreq(arr[i], K);
if (c > max) {
max = c;
ele = arr[i];
}
}
if (max == 0)
return -1;
else
return ele;
}
void findElement( int arr[], int K, int N)
{
int ele = findElementUtil(arr, N, K);
if (ele == -1)
cout << "-1" ;
else
cout << ele;
}
int main()
{
int K = 1;
int arr[] = { 1, 1111, 111, 11};
int N = sizeof (arr) / sizeof (arr[0]);
findElement(arr, K, N);
return 0;
}
|
Java
public class GFG {
static int countFreq( int N, int K)
{
int count = 0 ;
while (N > 0 )
{
if (N % 10 == K)
{
count++;
}
N = N / 10 ;
}
return count;
}
static int findElementUtil( int arr[], int N, int K)
{
int c;
int max;
int ele = 0 ;
max = 0 ;
for ( int i = 0 ; i < N; i++)
{
c = countFreq(arr[i], K);
if (c > max)
{
max = c;
ele = arr[i];
}
}
if (max == 0 )
return - 1 ;
else
return ele;
}
static void findElement( int arr[], int N, int K)
{
int ele = findElementUtil(arr, N, K);
if (ele == - 1 )
System.out.print( "-1" );
else
System.out.print(ele);
}
public static void main (String[] args)
{
int K = 1 ;
int arr[] = { 1 , 1111 , 111 , 11 };
int N = arr.length;
findElement(arr, N, K);
}
};
|
Python3
def countFreq(N, K):
count = 0
while (N > 0 ):
if (N % 10 = = K):
count + = 1
N = N / / 10
return count
def findElementUtil(arr, N, K):
c = 0
max = 0
ele = 0
for i in range (N):
c = countFreq(arr[i], K)
if (c > max ):
max = c
ele = arr[i]
if ( max = = 0 ):
return - 1
else :
return ele
def findElement(arr, N, K):
ele = findElementUtil(arr, N, K)
if (ele = = - 1 ):
print ( "-1" , end = "")
else :
print (ele)
if __name__ = = '__main__' :
K = 1
arr = [ 1 , 1111 , 111 , 11 ]
N = len (arr)
findElement(arr, N, K)
|
C#
using System;
class GFG {
static int countFreq( int N, int K)
{
int count = 0;
while (N > 0)
{
if (N % 10 == K)
{
count++;
}
N = N / 10;
}
return count;
}
static int findElementUtil( int []arr, int N, int K)
{
int c;
int max;
int ele = 0;
max = 0;
for ( int i = 0; i < N; i++)
{
c = countFreq(arr[i], K);
if (c > max)
{
max = c;
ele = arr[i];
}
}
if (max == 0)
return -1;
else
return ele;
}
static void findElement( int []arr, int N, int K)
{
int ele = findElementUtil(arr, N, K);
if (ele == -1)
Console.Write( "-1" );
else
Console.Write(ele);
}
public static void Main(String[] args)
{
int K = 1;
int []arr = { 1, 1111, 111, 11};
int N = arr.Length;
findElement(arr, N, K);
}
}
|
Javascript
<script>
function countFreq(N, K)
{
let count = 0;
while (N > 0)
{
if (N % 10 == K)
{
count++;
}
N = Math.floor(N / 10);
}
return count;
}
function findElementUtil(arr, N, K)
{
let c;
let max;
let ele = 0;
max = 0;
for (let i = 0; i < N; i++)
{
c = countFreq(arr[i], K);
if (c > max)
{
max = c;
ele = arr[i];
}
}
if (max == 0)
return -1;
else
return ele;
}
function findElement(arr, N, K)
{
let ele = findElementUtil(arr, N, K);
if (ele == -1)
document.write( "-1" );
else
document.write(ele);
}
let K = 3;
let arr = [1, 1111, 111, 11];
let N = arr.length;
findElement(arr, N, K};
</script>
|
Time Complexity: O(N * log10(N))
Auxiliary Space: O(1)
Another Approach:
- Start with an array arr[] of size N and an integer K.
- Initialize max_freq as 0 and result as -1.
- Traverse the array from the start to the end.
- For each element, count the number of occurrences of the digit K by iterating over its digits. You can do this by repeatedly dividing the number by 10 and checking the remainder for equality with K. If it’s equal, increment a frequency counter.
- Compare the frequency counter with max_freq. If it’s greater than max_freq, update max_freq with the frequency counter and result with the current element.
- After the traversal, if the max_freq is still 0, it means that no element contains the digit K, so return -1. Otherwise, return the result.
- Done.
C++
#include <iostream>
#include <vector>
using namespace std;
int findElement( int arr[], int N, int K) {
int max_freq = 0;
int result = -1;
for ( int i = 0; i < N; i++) {
int num = arr[i];
int freq = 0;
while (num > 0) {
if (num % 10 == K) {
freq++;
}
num /= 10;
}
if (freq > max_freq) {
max_freq = freq;
result = arr[i];
}
}
return result;
}
int main() {
int arr[] = { 1, 1111, 111, 11 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 1;
int result = findElement(arr, N, K);
if (result == -1) {
cout << "No element contains the digit " << K << endl;
}
else {
cout<<result<<endl;
}
return 0;
}
|
Java
import java.util.Scanner;
public class Main {
static int findElement( int [] arr, int N, int K) {
int maxFreq = 0 ;
int result = - 1 ;
for ( int i = 0 ; i < N; i++) {
int num = arr[i];
int freq = 0 ;
while (num > 0 ) {
if (num % 10 == K) {
freq++;
}
num /= 10 ;
}
if (freq > maxFreq) {
maxFreq = freq;
result = arr[i];
}
}
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int arr[] = { 1 , 1111 , 111 , 11 };
int N = arr.length;
int K = 1 ;
int result = findElement(arr, N, K);
System.out.println(result);
}
}
|
Python3
def findElement(arr, N, K):
max_freq = 0
result = - 1
for i in range (N):
num = arr[i]
freq = 0
while num > 0 :
if num % 10 = = K:
freq + = 1
num / / = 10
if freq > max_freq:
max_freq = freq
result = arr[i]
return result
arr = [ 1 , 1111 , 111 , 11 ]
N = len (arr)
K = 1
result = findElement(arr, N, K)
if result = = - 1 :
print ( "No element contains the digit" , K)
else :
print (result)
|
C#
using System;
namespace FindElementWithMaxDigit
{
class Program
{
static int FindElement( int [] arr, int N, int K)
{
int maxFreq = 0;
int result = -1;
for ( int i = 0; i < N; i++)
{
int num = arr[i];
int freq = 0;
while (num > 0)
{
if (num % 10 == K)
{
freq++;
}
num /= 10;
}
if (freq > maxFreq)
{
maxFreq = freq;
result = arr[i];
}
}
return result;
}
static void Main( string [] args)
{
int [] arr = { 1, 1111, 111, 11 };
int N = arr.Length;
int K = 1;
int result = FindElement(arr, N, K);
if (result == -1)
{
Console.WriteLine($ "No element contains the digit {K}" );
}
else
{
Console.WriteLine(result);
}
}
}
}
|
Javascript
function findElement(arr, N, K) {
let max_freq = 0;
let result = -1;
for (let i = 0; i < N; i++) {
let num = arr[i];
let freq = 0;
while (num > 0) {
if (num % 10 == K) {
freq += 1;
}
num = Math.floor(num / 10);
}
if (freq > max_freq) {
max_freq = freq;
result = arr[i];
}
}
return result;
}
let arr = [1, 1111, 111, 11];
let N = arr.length;
let K = 1;
let result = findElement(arr, N, K);
if (result == -1) {
console.log( "No element contains the digit" , K);
} else {
console.log(result);
}
|
Time Complexity: O(N*M)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...