Count frequency of digit K in given Array - GeeksforGeeks

07 Dec, 2021
Given an array arr[] of integers of size N and a single digit integer K. The task is to find the total count of occurrences of the digit K in the array
Examples:
Input: arr[] = {15, 66, 26, 91}, K = 6Output: 3Explanation: Occurrences of 6 in each array elements are: 0, 2, 1, 0 respectively.Therefore, total occurrences = 0 + 2 + 1 + 0 = 3
Input: arr[] = {20, 21, 0}, K = 0Output: 2
Approach: The idea is to traverse the array and for every individual array element, count the occurrences of the digit K in it and update the total count. Follow the steps below to solve the problem:
Initialize the total occurrences of digit K, say count, 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.Add the count to the total sum.Return the total sum as answer.
Initialize the total occurrences of digit K, say count, 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.
Add the count to the total sum.
Return the total sum as answer.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ code to count frequency// of digit K in given Array #include <bits/stdc++.h>using namespace std; // Function to count occurrences// in an elementint countOccurrences(int num, int K){    // If num is 0    if (K == 0 && num == 0)        return 1;     // Initialize count    int count = 0;     // Count for occurrences of digit K    while (num > 0) {        if (num % 10 == K)            count++;        num /= 10;    }    return count;} // Function to return sum of// total occurrences of digit Kint totalOccurrences(int arr[], int N, int K){     // Initialize sum    int sum = 0;     // Traverse the array    for (int i = 0; i < N; i++) {        sum += countOccurrences(arr[i], K);    }     return sum;} // Driver Codeint main(){    int arr[] = { 15, 66, 26, 91 };    int K = 6;     int N = sizeof(arr) / sizeof(arr[0]);     cout << totalOccurrences(arr, N, K);    return 0;}
// Java program for the above approachimport java.io.*;class GFG {   // Function to count occurrences  // in an element  static int countOccurrences(int num, int K)  {     // If num is 0    if (K == 0 && num == 0)      return 1;     // Initialize count    int count = 0;     // Count for occurrences of digit K    while (num > 0) {      if (num % 10 == K)        count++;      num /= 10;    }    return count;  }   // Function to return sum of  // total occurrences of digit K  static int totalOccurrences(int arr[], int N, int K)  {     // Initialize sum    int sum = 0;     // Traverse the array    for (int i = 0; i < N; i++) {      sum += countOccurrences(arr[i], K);    }     return sum;  }   // Driver Code  public static void main (String[] args)  {         int arr[] = { 15, 66, 26, 91 };    int K = 6;    int N = arr.length;    System.out.println( totalOccurrences(arr, N, K));  }} // This code is contributed by Potta Lokesh
# Python3 code to count frequency# of digit K in given array # Function to count occurrences# in an elementdef countOccurrences(num, K):     # If num is 0    if (K == 0 and num == 0):        return 1     # Initialize count    count = 0     # Count for occurrences of digit K    while (num > 0):        if (num % 10 == K):            count += 1                     num = (num // 10)             return count # Function to return sum of# total occurrences of digit Kdef totalOccurrences(arr, N, K):     # Initialize sum    sum = 0     # Traverse the array    for i in range(N):        sum += countOccurrences(arr[i], K)     return sum # Driver Codearr = [ 15, 66, 26, 91 ]K = 6 N = len(arr) print(totalOccurrences(arr, N, K)) # This code is contributed by saurabh_jaiswal.
// C# program for the above approachusing System;class GFG {   // Function to count occurrences  // in an element  static int countOccurrences(int num, int K)  {     // If num is 0    if (K == 0 && num == 0)      return 1;     // Initialize count    int count = 0;     // Count for occurrences of digit K    while (num > 0) {      if (num % 10 == K)        count++;      num /= 10;    }    return count;  }   // Function to return sum of  // total occurrences of digit K  static int totalOccurrences(int []arr, int N, int K)  {     // Initialize sum    int sum = 0;     // Traverse the array    for (int i = 0; i < N; i++) {      sum += countOccurrences(arr[i], K);    }     return sum;  }   // Driver Code  public static void Main ()  {         int []arr = { 15, 66, 26, 91 };    int K = 6;    int N = arr.Length;    Console.Write( totalOccurrences(arr, N, K));  }} // This code is contributed by Samim Hossain Mondal.
<script> // Javascript code to count frequency// of digit K in given Array // Function to count occurrences// in an elementfunction countOccurrences(num, K){     // If num is 0    if (K == 0 && num == 0)        return 1;     // Initialize count    let count = 0;     // Count for occurrences of digit K    while (num > 0) {        if (num % 10 == K)            count++;        num = Math.floor(num / 10);    }    return count;} // Function to return sum of// total occurrences of digit Kfunction totalOccurrences(arr, N, K) {     // Initialize sum    let sum = 0;     // Traverse the array    for (let i = 0; i < N; i++) {        sum += countOccurrences(arr[i], K);    }     return sum;} // Driver Codelet arr = [15, 66, 26, 91];let K = 6; let N = arr.lengthdocument.write(totalOccurrences(arr, N, K)); // This code is contributed by saurabh_jaiswal.</script>
3
Time Complexity: O(N*D) where D is the maximum number of digit in an array elementAuxiliary Space: O(1)
lokeshpotta20
samim2000
_saurabh_jaiswal
frequency-counting
Arrays
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count pairs with given sum
Chocolate Distribution Problem
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
Find duplicates in O(n) time and O(1) extra space | Set 1
Find subarray with given sum | Set 1 (Nonnegative Numbers)
Remove duplicates from sorted array
Move all negative numbers to beginning and positive to end with constant extra space
Merge Overlapping Intervals