Find the array element having maximum frequency of the digit K

• Difficulty Level : Easy
• Last Updated : 08 Jun, 2021

Given an array arr[] of size N and an integer K, the task is to find an array element which contains the digit K 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, the count the occurrences of the digit K in it. Follow the steps below to solve the problem:

1. Initialize the max frequency of digit K, say maxfreq, as 0.
2. Traverse the given array from the start element till the end.
3. 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++

 // C++ program for the above approach #include using namespace std; // Function to find the count of// digits, k in the given number nint countFreq(int N, int K){     // Stores count of occurrence    // of digit K in N    int count = 0;     // Iterate over digits of N    while (N > 0) {         // If current digit is k        if (N % 10 == K) {             // Update count            count++;        }         // Update N        N = N / 10;    }    return count;} // Utility function to find an array element// having maximum frequency of digit kint findElementUtil(int arr[], int N, int K){     // Stores frequency of    // digit K in arr[i]    int c;     // Stores maximum frequency of    // digit K in the array    int max;     // Stores an array element having    // maximum frequency of digit k    int ele;     // Initialize max    max = 0;     // Traverse the array    for (int i = 0; i < N; i++) {         // Count the frequency of        // digit k in arr[i]        c = countFreq(arr[i], K);         // Update max with maximum        // frequency found so far        if (c > max) {            max = c;             // Update ele            ele = arr[i];        }    }     // If there is no array element    // having digit k in it    if (max == 0)        return -1;    else        return ele;} // Function to find an array element// having maximum frequency of digit kvoid findElement(int arr[], int N, int K){     // Stores an array element having    // maximum frequency of digit k    int ele = findElementUtil(arr, N, K);     // If there is no element found    // having digit k in it    if (ele == -1)        cout << "-1";     else         // Print the element having max        // frequency of digit k        cout << ele;} // Driver Codeint main(){     // The digit whose max    // occurrence has to be found    int K = 3;     // Given array    int arr[] = { 3, 77, 343, 456 };     // Size of array    int N = sizeof(arr) / sizeof(arr);     // Function Call    findElement(arr, K, N);     return 0;}

Java

 // Java program for the above approachclass GFG {         // Function to find the count of    // digits, k in the given number n    static int countFreq(int N, int K)    {             // Stores count of occurrence        // of digit K in N        int count = 0;             // Iterate over digits of N        while (N > 0)        {                 // If current digit is k            if (N % 10 == K)            {                     // Update count                count++;            }                 // Update N            N = N / 10;        }        return count;    }         // Utility function to find an array element    // having maximum frequency of digit k    static int findElementUtil(int arr[], int N, int K)    {             // Stores frequency of        // digit K in arr[i]        int c;             // Stores maximum frequency of        // digit K in the array        int max;             // Stores an array element having        // maximum frequency of digit k        int ele = 0;             // Initialize max        max = 0;             // Traverse the array        for (int i = 0; i < N; i++)        {                 // Count the frequency of            // digit k in arr[i]            c = countFreq(arr[i], K);                 // Update max with maximum            // frequency found so far            if (c > max)            {                max = c;                     // Update ele                ele = arr[i];            }        }             // If there is no array element        // having digit k in it        if (max == 0)            return -1;        else            return ele;    }         // Function to find an array element    // having maximum frequency of digit k    static void findElement(int arr[], int N, int K)    {             // Stores an array element having        // maximum frequency of digit k        int ele = findElementUtil(arr, N, K);             // If there is no element found        // having digit k in it        if (ele == -1)            System.out.print("-1");             else                 // Print the element having max            // frequency of digit k            System.out.print(ele);    }         // Driver Code    public static void main (String[] args)    {             // The digit whose max        // occurrence has to be found        int K = 3;             // Given array        int arr[] = { 3, 77, 343, 456 };             // Size of array        int N = arr.length;             // Function Call        findElement(arr, K, N);      }} // This code is contributed by AnkThon

Python3

 # Python3 program for the above approach # Function to find the count of# digits, k in the given number ndef countFreq(N, K):     # Stores count of occurrence    # of digit K in N    count = 0     # Iterate over digits of N    while (N > 0):         # If current digit is k        if (N % 10 == K):             # Update count            count += 1         # Update N        N = N // 10    return count # Utility function to find an array element# having maximum frequency of digit kdef findElementUtil(arr, N, K):     # Stores frequency of    # digit K in arr[i]    c  = 0     # Stores maximum frequency of    # digit K in the array    max = 0     # Stores an array element having    # maximum frequency of digit k    ele = 0     # Initialize max    # max = 0     # Traverse the array    for i in range(N):         # Count the frequency of        # digit k in arr[i]        c = countFreq(arr[i], K)         # Update max with maximum        # frequency found so far        if (c > max):            max = c             # Update ele            ele = arr[i]     # If there is no array element    # having digit k in it    if (max == 0):        return -1    else:        return ele # Function to find an array element# having maximum frequency of digit kdef findElement(arr, N, K):     # Stores an array element having    # maximum frequency of digit k    ele = findElementUtil(arr, N, K)     # If there is no element found    # having digit k in it    if (ele == -1):        print("-1", end = "")     else:         # Print element having max        # frequency of digit k        print(ele) # Driver Codeif __name__ == '__main__':     # The digit whose max    # occurrence has to be found    K = 3     # Given array    arr = [3, 77, 343, 456]     # Size of array    N = len(arr)     # Function Call    findElement(arr, K, N) # This code is contributed by mohit kumar 29

C#

 // C# program for the above approachusing System; class GFG {   // Function to find the count of  // digits, k in the given number n  static int countFreq(int N, int K)  {     // Stores count of occurrence    // of digit K in N    int count = 0;     // Iterate over digits of N    while (N > 0)    {       // If current digit is k      if (N % 10 == K)      {         // Update count        count++;      }       // Update N      N = N / 10;    }    return count;  }   // Utility function to find an array element  // having maximum frequency of digit k  static int findElementUtil(int []arr, int N, int K)  {     // Stores frequency of    // digit K in arr[i]    int c;     // Stores maximum frequency of    // digit K in the array    int max;     // Stores an array element having    // maximum frequency of digit k    int ele = 0;     // Initialize max    max = 0;     // Traverse the array    for (int i = 0; i < N; i++)    {       // Count the frequency of      // digit k in arr[i]      c = countFreq(arr[i], K);       // Update max with maximum      // frequency found so far      if (c > max)      {        max = c;         // Update ele        ele = arr[i];      }    }     // If there is no array element    // having digit k in it    if (max == 0)      return -1;    else      return ele;  }   // Function to find an array element  // having maximum frequency of digit k  static void findElement(int []arr, int N, int K)  {     // Stores an array element having    // maximum frequency of digit k    int ele = findElementUtil(arr, N, K);     // If there is no element found    // having digit k in it    if (ele == -1)      Console.Write("-1");     else       // Print the element having max      // frequency of digit k      Console.Write(ele);  }   // Driver Code  public static void Main(String[] args)  {     // The digit whose max    // occurrence has to be found    int K = 3;     // Given array    int []arr = { 3, 77, 343, 456 };     // Size of array    int N = arr.Length;     // Function Call    findElement(arr, K, N);    }} // This code is contributed by 29AjayKumar

Javascript


Output:
343

Time Complexity: O(N * log10(N))
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up