Find the array element having maximum frequency of the digit K | C ++ program for the above approach ; Function to find the count of digits , k in the given number n ; Stores count of occurrence of digit K in N ; Iterate over digits of N ; If current digit is k ; Update count ; Update N ; Utility function to find an array element having maximum frequency of digit k ; Stores frequency of digit K in arr [ i ] ; Stores maximum frequency of digit K in the array ; Stores an array element having maximum frequency of digit k ; Initialize max ; Traverse the array ; Count the frequency of digit k in arr [ i ] ; Update max with maximum frequency found so far ; Update ele ; If there is no array element having digit k in it ; Function to find an array element having maximum frequency of digit k ; Stores an array element having maximum frequency of digit k ; If there is no element found having digit k in it ; Print the element having max frequency of digit k ; Driver Code ; The digit whose max occurrence has to be found ; Given array ; Size of array ; Function Call
#include <bits/stdc++.h> NEW_LINE 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 ; int max ; 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 N , int K ) { int ele = findElementUtil ( arr , N , K ) ; if ( ele == -1 ) cout << " - 1" ; else cout << ele ; } int main ( ) { int K = 3 ; int arr [ ] = { 3 , 77 , 343 , 456 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findElement ( arr , K , N ) ; return 0 ; }