Count frequency of digit K in given Array
Last Updated :
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 = 6
Output: 3
Explanation: 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 = 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++
#include <bits/stdc++.h>
using namespace std;
int countOccurrences( int num, int K)
{
if (K == 0 && num == 0)
return 1;
int count = 0;
while (num > 0) {
if (num % 10 == K)
count++;
num /= 10;
}
return count;
}
int totalOccurrences( int arr[], int N, int K)
{
int sum = 0;
for ( int i = 0; i < N; i++) {
sum += countOccurrences(arr[i], K);
}
return sum;
}
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
import java.io.*;
class GFG {
static int countOccurrences( int num, int K)
{
if (K == 0 && num == 0 )
return 1 ;
int count = 0 ;
while (num > 0 ) {
if (num % 10 == K)
count++;
num /= 10 ;
}
return count;
}
static int totalOccurrences( int arr[], int N, int K)
{
int sum = 0 ;
for ( int i = 0 ; i < N; i++) {
sum += countOccurrences(arr[i], K);
}
return sum;
}
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));
}
}
|
Python3
def countOccurrences(num, K):
if (K = = 0 and num = = 0 ):
return 1
count = 0
while (num > 0 ):
if (num % 10 = = K):
count + = 1
num = (num / / 10 )
return count
def totalOccurrences(arr, N, K):
sum = 0
for i in range (N):
sum + = countOccurrences(arr[i], K)
return sum
arr = [ 15 , 66 , 26 , 91 ]
K = 6
N = len (arr)
print (totalOccurrences(arr, N, K))
|
C#
using System;
class GFG {
static int countOccurrences( int num, int K)
{
if (K == 0 && num == 0)
return 1;
int count = 0;
while (num > 0) {
if (num % 10 == K)
count++;
num /= 10;
}
return count;
}
static int totalOccurrences( int []arr, int N, int K)
{
int sum = 0;
for ( int i = 0; i < N; i++) {
sum += countOccurrences(arr[i], K);
}
return sum;
}
public static void Main ()
{
int []arr = { 15, 66, 26, 91 };
int K = 6;
int N = arr.Length;
Console.Write( totalOccurrences(arr, N, K));
}
}
|
Javascript
<script>
function countOccurrences(num, K)
{
if (K == 0 && num == 0)
return 1;
let count = 0;
while (num > 0) {
if (num % 10 == K)
count++;
num = Math.floor(num / 10);
}
return count;
}
function totalOccurrences(arr, N, K) {
let sum = 0;
for (let i = 0; i < N; i++) {
sum += countOccurrences(arr[i], K);
}
return sum;
}
let arr = [15, 66, 26, 91];
let K = 6;
let N = arr.length
document.write(totalOccurrences(arr, N, K));
</script>
|
Time Complexity: O(N*D) where D is the maximum number of digit in an array element
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...