Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Find the frequency of a digit in a number
Next article icon

Count frequency of digit K in given Array

Last Updated : 07 Dec, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

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:

  1. Initialize the total occurrences of digit K, say count, 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.
  4. Add the count to the total sum.
  5. 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> 

Output
3

Time Complexity: O(N*D) where D is the maximum number of digit in an array element
Auxiliary Space: O(1)


Next Article
Find the frequency of a digit in a number

V

vansikasharma1329
Improve
Article Tags :
  • Misc
  • DSA
  • Arrays
  • frequency-counting
Practice Tags :
  • Arrays
  • Misc

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 inversions of size k in a given array
    Given an array of n distinct integers [Tex]a_{1}, a_{2}, ..., a_{n} [/Tex]and an integer k. Find out the number of sub-sequences of a such that [Tex]a_{i_{1}} > a_{i_{2}} > ... > a_{i_{k}} [/Tex], and [Tex]1 <= i_{1} < i_{2} < ... < i_{k} <= n [/Tex]. In other words output th
    14 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
  • Find the frequency of a digit in a number
    Given a number N and a digit D. Write a program to find how many times the digit D appears in the number N. Examples : Input: N = 1122322 , D = 2 Output: 4 Input: N = 346488 , D = 9 Output: 0 The idea to solve this problem is to keep extracting digits from the number N and check the extracted digits
    4 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
  • Count array elements having exactly K divisors
    Given an array arr[] consisting of N integers and an integer K, the task is to count the number of array elements having exactly K divisors. Examples: Input: N = 5, arr[] = { 3, 6, 2, 9, 4 }, K = 2Output: 2Explanation: arr[0] (= 3) and arr[2] (= 2) have exactly 2 divisors. Input: N = 5, arr[] = { 3,
    14 min read
  • Count array elements whose all distinct digits appear in K
    Given an array arr[] consisting of N positive integers and a positive integer K, the task is to find the count of array elements whose distinct digits are a subset of the digits of K. Examples: Input: arr[] = { 1, 12, 1222, 13, 2 }, K = 12Output: 4Explanation: Distinct Digits of K are { 1, 2 } Disti
    15+ min read
  • Construct a frequency array of digits of the values obtained from x^1, x^2, ........, x^n
    Given are two integers (x and n). The task is to find an array such that it contains the frequency of index numbers occurring in (x^1, x^2, ...., x^(n-1), x^(n) ). Examples: Input: x = 15, n = 3 Output: 0 1 2 2 0 3 0 1 0 0 Numbers x^1 to x^n are 15, 225, 3375. So frequency array is 0 1 2 2 0 3 0 1 0
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences