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 = 6
Output: 3
Explanation: Occurrences of 6 in each array elements are: 0, 2, 1, 0 respectively.
Therefore, total occurrences = 0 + 2 + 1 + 0 = 3Input: arr[] = {20, 21, 0}, K = 0
Output: 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.
Below is the implementation of the above approach:
C++
// C++ code to count frequency // of digit K in given Array #include <bits/stdc++.h> using namespace std; // Function to count occurrences // in an element 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 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 int 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
// Java program for the above approach import 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
# Python3 code to count frequency # of digit K in given array # Function to count occurrences # in an element def 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 K def totalOccurrences(arr, N, K): # Initialize sum sum = 0 # Traverse the array for i in range (N): sum + = countOccurrences(arr[i], K) return sum # Driver Code arr = [ 15 , 66 , 26 , 91 ] K = 6 N = len (arr) print (totalOccurrences(arr, N, K)) # This code is contributed by saurabh_jaiswal. |
C#
// C# program for the above approach using 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. |
Javascript
<script> // Javascript code to count frequency // of digit K in given Array // Function to count occurrences // in an element function 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 K function 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 Code let arr = [15, 66, 26, 91]; let K = 6; let N = arr.length document.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 element
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!