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++
// 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>
Time Complexity: O(N*D) where D is the maximum number of digit in an array element
Auxiliary Space: O(1)
Similar Reads
Count array elements having sum of digits equal to K Given an array arr[] of size N, the task is to count the number of array elements whose sum of digits is equal to K. Examples: Input: arr[] = {23, 54, 87, 29, 92, 62}, K = 11Output: 2Explanation: 29 = 2 + 9 = 1192 = 9 + 2 = 11 Input: arr[]= {11, 04, 57, 99, 98, 32}, K = 18Output: 1 Approach: Follow
6 min read
Count of unique digits in a given number N Given a number N, the task is to count the number of unique digits in the given number. Examples: Input: N = 22342 Output: 2 Explanation:The digits 3 and 4 occurs only once. Hence, the output is 2. Input: N = 99677 Output: 1Explanation:The digit 6 occurs only once. Hence, the output is 1. Naive Appr
6 min read
Count digits present in each element of a given Matrix Given a matrix arr[][] of dimensions M * N, the task is to count the number of digits of every element present in the given matrix. Examples: Input: arr[][] = { { 27, 173, 5 }, { 21, 6, 624 }, { 5, 321, 49 } }Output: 2 3 12 1 31 3 2 Input: arr[][] = { {11, 12, 33 }, { 64, 57, 61 }, { 74, 88, 39 } }O
5 min read
Count of K-countdowns in an Array Given an array arr[] of length N and a number K, the task is to count the number of K-countdowns in the array. A contiguous subarray is said to be a K-countdown if it is of length K and contains the integers K, K-1, K-2, ..., 2, 1 in that order. For example, [4, 3, 2, 1] is 4-countdown and [6, 5, 4,
7 min read
Count of repeating digits in a given Number Given a number N, the task is to count the total number of repeating digits in the given number. Examples: Input: N = 99677 Output: 2Explanation:In the given number only 9 and 7 are repeating, hence the answer is 2. Input: N = 12Output: 0Explanation:In the given number no digits are repeating, hence
12 min read