Given a number N and a digit D, the task is to count the occurrences of D in N.
Examples:
Input: N = 25, D = 2
Output: 1
Input: N = 100, D = 0
Output: 2
The basic idea is to loop through the digits of the number and keep a count of the occurrences of each digit in a hash table. Then, we can simply look up the count of the digit we are interested in.
Here are the steps in more detail:
- Initialize a hash table with keys 0 to 9 and values 0.
- Convert the number to a string, so that we can loop through its digits.
- Loop through the digits of the number, and for each digit, increment the count in the hash table corresponding to that digit.
- Return the count.
Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countOccurrences( long long int n, int d)
{
unordered_map< int , int > digitCounts;
for ( int i = 0; i <= 9; i++) {
digitCounts[i] = 0;
}
string numString = to_string(n);
for ( int i = 0; i < numString.length(); i++) {
int digit = numString[i] - '0' ;
digitCounts[digit]++;
}
return digitCounts[d];
}
int main()
{
int d = 2;
long long int n = 214215421;
cout << countOccurrences(n, d) << endl;
return 0;
}
|
Java
import java.util.HashMap;
public class Main {
public static int countOccurrences( long n, int d) {
HashMap<Integer, Integer> digitCounts = new HashMap<>();
for ( int i = 0 ; i <= 9 ; i++) {
digitCounts.put(i, 0 );
}
String numString = Long.toString(n);
for ( int i = 0 ; i < numString.length(); i++) {
int digit = Character.getNumericValue(numString.charAt(i));
digitCounts.put(digit, digitCounts.get(digit) + 1 );
}
return digitCounts.get(d);
}
public static void main(String[] args) {
int d = 2 ;
long n = 214215421 ;
System.out.println(countOccurrences(n, d));
}
}
|
Python3
def countOccurrences(n, d):
digitCounts = {i: 0 for i in range ( 10 )}
numString = str (n)
for digit in numString:
digitCounts[ int (digit)] + = 1
return digitCounts[d]
if __name__ = = "__main__" :
d = 2
n = 214215421
print (countOccurrences(n, d))
|
Javascript
function countOccurrences(n, d) {
let digitCounts = {};
for (let i = 0; i < 10; i++) {
digitCounts[i] = 0;
}
let numString = n.toString();
for (let i = 0; i < numString.length; i++) {
digitCounts[parseInt(numString.charAt(i))] += 1;
}
return digitCounts[d];
}
let d = 2;
let n = 214215421;
console.log(countOccurrences(n, d));
|
C#
using System;
using System.Collections.Generic;
class MainClass {
static int CountOccurrences( long n, int d) {
Dictionary< int , int > digitCounts = new Dictionary< int , int >();
for ( int i = 0; i <= 9; i++) {
digitCounts[i] = 0;
}
string numString = n.ToString();
for ( int i = 0; i < numString.Length; i++) {
int digit = numString[i] - '0' ;
digitCounts[digit]++;
}
return digitCounts[d];
}
static void Main() {
int d = 2;
long n = 214215421;
Console.WriteLine(CountOccurrences(n, d));
}
}
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach: Take out the digits one by one in N and check if this digit is equal to D. If equal, then increment the count by 1. In the end, print the count.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
long long int countoccurrences( long long int n,
int d)
{
long long int count = 0;
while (n > 0) {
count = (n % 10 == d)
? count + 1
: count;
n = n / 10;
}
return count;
}
int main()
{
int d = 2;
long long int n = 214215421;
cout << countOccurrences(n, d)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int countoccurrences( int n,
int d)
{
int count = 0 ;
while (n > 0 )
{
count = (n % 10 == d) ?
count + 1 : count;
n = n / 10 ;
}
return count;
}
public static void main(String args[])
{
int d = 2 ;
int n = 214215421 ;
System.out.println(countOccurrences(n, d));
}
}
|
Python3
def countoccurrences(n, d):
count = 0
while (n > 0 ):
if (n % 10 = = d):
count = count + 1
n = n / / 10
return count
d = 2
n = 214215421
print (countOccurrences(n, d))
|
C#
using System;
class GFG
{
static int countoccurrences( int n,
int d)
{
int count = 0;
while (n > 0)
{
count = (n % 10 == d) ?
count + 1 : count;
n = n / 10;
}
return count;
}
public static void Main()
{
int d = 2;
int n = 214215421;
Console.WriteLine(countOccurrences(n, d));
}
}
|
Javascript
<script>
function countoccurrences(n, d)
{
let count = 0;
while (n > 0) {
count = (n % 10 == d)
? count + 1
: count;
n = parseInt(n / 10);
}
return count;
}
let d = 2;
let n = 214215421;
document.write(countOccurrences(n, d));
</script>
|
Time Complexity : O(log(n)), where n is the input variable
Auxiliary Space : O(1)