# Find the frequency of each digit in a number

The given problem requires finding the frequency of each digit in a given integer number, including negative numbers. For example, if the input number is 1234, the output should show that the digits 1, 2, 3, and 4 each occur once in the number.

## Explanation with Example

Let's take the first test case, which is the number 1234:

```Number: 1234
0: 0 (not present)
1: 1 (appears once)
2: 1 (appears once)
3: 1 (appears once)
4: 1 (appears once)
5: 0 (not present)
6: 0 (not present)
7: 0 (not present)
8: 0 (not present)
9: 0 (not present)```
1. Initialize an array `result` to store the frequency of each digit. Since there are 10 possible digits (0 to 9), the array will be used to store the frequency of each digit.
2. The `occurrence` function is called with the number 1234 and the `result` array.
3. In the `occurrence` function, a while loop is used to extract each digit from the number. This is done using the modulo operation (`number % 10`), which gives the last digit of the number.
4. Since the input number can be negative, a check is performed to make the digit positive (`temp = -temp`) if it is negative.
5. The frequency of the digit is then incremented in the `result` array by using the digit as an index (`result[temp]++`).
6. The number is updated by removing the last digit (`number /= 10`), and the process is repeated until all digits have been processed.
7. After the `occurrence` function is executed, the main function prints the result, showing the frequency of each digit for the given number.

## Standard Pseudocode

``````function occurrence(number, result):
while number is not 0:
temp = number % 10
if temp is less than 0:
temp = -temp
result[temp] = result[temp] + 1
number = number / 10

function frequency(number):
create an array result and initialize all elements to 0
print "Number: ", number
call occurrence(number, result)
for i from 0 to 9:
if result[i] is not 0:
print i, " : ", result[i]
``````

## Algorithm Explanation

1. The `occurrence` function is responsible for finding the frequency of each digit in the given number. It iterates through the digits by repeatedly dividing the number by 10 and extracting the last digit using the modulo operation. The frequency of each digit is updated in the `result` array.

2. The `frequency` function is the main function that handles the process for each test case. It initializes the `result` array, calls the `occurrence` function, and then prints the result.

## Code Solution

Here given code implementation process.

``````//C Program
//Find the frequency of each digit in a number
#include <stdio.h>

//Find the occurrence of digits in number
void occurrence(int number, int result[])
{
int temp = 0;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
temp = -temp;
}
result[temp]++;
number /= 10;
}
}
void frequency(int number)
{
//This is used to store result of number digit frequency
int result;
//Set initial frequency of digit
for (int i = 0; i < 10; ++i)
{
result[i] = 0;
}
printf("\nNumber :  %d", number);
occurrence(number, result);
for (int i = 0; i < 10; ++i)
{
if (result[i] != 0)
{
printf("\n %d  : %d", i, result[i]);
}
}
printf("\n");
}
int main()
{
//Test Cases
frequency(1234);
frequency(7831432);
frequency(5121112);
frequency(-4211221);
frequency(1177714);
return 0;
}``````

#### Output

``````Number :  1234
1  : 1
2  : 1
3  : 1
4  : 1

Number :  7831432
1  : 1
2  : 1
3  : 2
4  : 1
7  : 1
8  : 1

Number :  5121112
1  : 4
2  : 2
5  : 1

Number :  -4211221
1  : 3
2  : 3
4  : 1

Number :  1177714
1  : 3
4  : 1
7  : 3``````
``````/*
C++ Program
Find the frequency of each digit in a number
*/
#include<iostream>

using namespace std;
class MyNumber
{
public:
//Find the occurrence of digits in number
void occurrence(int number, int result[])
{
int temp = 0;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
temp = -temp;
}
result[temp]++;
number /= 10;
}
}
void frequency(int number)
{
int *result = new int;
//Set initial frequency of digit
for (int i = 0; i < 10; ++i)
{
result[i] = 0;
}
cout << "\nNumber : " << number << "";
this->occurrence(number, result);
for (int i = 0; i < 10; ++i)
{
if (result[i] != 0)
{
cout << "\n " << i << " : " << result[i] << "";
}
}
cout << "\n";
}
};
int main()
{
MyNumber obj =  MyNumber();
//Test Case
obj.frequency(1234);
obj.frequency(7831432);
obj.frequency(5121112);
obj.frequency(-4211221);
obj.frequency(1177714);
return 0;
}``````

#### Output

``````Number : 1234
1 : 1
2 : 1
3 : 1
4 : 1

Number : 7831432
1 : 1
2 : 1
3 : 2
4 : 1
7 : 1
8 : 1

Number : 5121112
1 : 4
2 : 2
5 : 1

Number : -4211221
1 : 3
2 : 3
4 : 1

Number : 1177714
1 : 3
4 : 1
7 : 3``````
``````/*
Java Program
Find the frequency of each digit in a number
*/
public class MyNumber
{
//Find the occurrence of digits in number
public void occurrence(int number, int[] result)
{
int temp = 0;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
temp = -temp;
}
result[temp]++;
number /= 10;
}
}
public void frequency(int number)
{
//This is used to store result of number digit frequency
int[] result = new int;
//Set initial frequency of digit
for (int i = 0; i < 10; ++i)
{
result[i] = 0;
}
System.out.print("\nNumber : " + number + "");
occurrence(number, result);
for (int i = 0; i < 10; ++i)
{
if (result[i] != 0)
{
System.out.print("\n " + i + " : " + result[i] + "");
}
}
System.out.print("\n");
}
public static void main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Case
obj.frequency(1234);
obj.frequency(7831432);
obj.frequency(5121112);
obj.frequency(-4211221);
obj.frequency(1177714);
}
}``````

#### Output

``````Number : 1234
1 : 1
2 : 1
3 : 1
4 : 1

Number : 7831432
1 : 1
2 : 1
3 : 2
4 : 1
7 : 1
8 : 1

Number : 5121112
1 : 4
2 : 2
5 : 1

Number : -4211221
1 : 3
2 : 3
4 : 1

Number : 1177714
1 : 3
4 : 1
7 : 3``````
``````/*
C# Program
Find the frequency of each digit in a number
*/
using System;
public class MyNumber
{
//Find the occurrence of digits in number
public void occurrence(int number, int[] result)
{
int temp = 0;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
temp = -temp;
}
result[temp]++;
number /= 10;
}
}
public void frequency(int number)
{
int[] result = new int;
//Set initial frequency of digit
for (int i = 0; i < 10; i++)
{
result[i] = 0;
}
Console.Write("\nNumber : " + number + "");
occurrence(number, result);
for (int i = 0; i < 10; i++)
{
if (result[i] != 0)
{
Console.Write("\n " + i + " : " + result[i] + "");
}
}
Console.Write("\n");
}
public static void Main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Case
obj.frequency(1234);
obj.frequency(7831432);
obj.frequency(5121112);
obj.frequency(-4211221);
obj.frequency(1177714);
}
}``````

#### Output

``````Number : 1234
1 : 1
2 : 1
3 : 1
4 : 1

Number : 7831432
1 : 1
2 : 1
3 : 2
4 : 1
7 : 1
8 : 1

Number : 5121112
1 : 4
2 : 2
5 : 1

Number : -4211221
1 : 3
2 : 3
4 : 1

Number : 1177714
1 : 3
4 : 1
7 : 3``````
``````<?php
/*
Php Program
Find the frequency of each digit in a number
*/
class MyNumber
{
//Find the occurrence of digits in number
public 	function occurrence(\$number, & \$result)
{
\$temp = 0;
while (\$number != 0)
{
\$temp = \$number % 10;
if (\$temp < 0)
{
\$temp = -\$temp;
}
\$result[\$temp]++;
\$number = intval(\$number / 10);
}
}
public 	function frequency(\$number)
{
//This is used to store result of number digit frequency
\$result = array_fill(0, 10, 0);
echo("\nNumber : ". \$number ."");
\$this->occurrence(\$number, \$result);
for (\$i = 0; \$i < 10; ++\$i)
{
if (\$result[\$i] != 0)
{
echo("\n ". \$i ." : ". \$result[\$i] ."");
}
}
echo("\n");
}
}

function main()
{
\$obj = new MyNumber();
//Test Case
\$obj->frequency(1234);
\$obj->frequency(7831432);
\$obj->frequency(5121112);
\$obj->frequency(-4211221);
\$obj->frequency(1177714);
}
main();``````

#### Output

``````Number : 1234
1 : 1
2 : 1
3 : 1
4 : 1

Number : 7831432
1 : 1
2 : 1
3 : 2
4 : 1
7 : 1
8 : 1

Number : 5121112
1 : 4
2 : 2
5 : 1

Number : -4211221
1 : 3
2 : 3
4 : 1

Number : 1177714
1 : 3
4 : 1
7 : 3``````
``````/*
Node Js Program
Find the frequency of each digit in a number
*/
class MyNumber
{
//Find the occurrence of digits in number
occurrence(number, result)
{
var temp = 0;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
temp = -temp;
}
result[temp]++;
number = parseInt(number / 10);
}
}
frequency(number)
{
//This is used to store result of number digit frequency
var result = Array(10).fill(0);
process.stdout.write("\nNumber : " + number + "");
this.occurrence(number, result);
for (var i = 0; i < 10; ++i)
{
if (result[i] != 0)
{
process.stdout.write("\n " + i + " : " + result[i] + "");
}
}
process.stdout.write("\n");
}
}

function main(args)
{
var obj = new MyNumber();
//Test Case
obj.frequency(1234);
obj.frequency(7831432);
obj.frequency(5121112);
obj.frequency(-4211221);
obj.frequency(1177714);
}
main();``````

#### Output

``````Number : 1234
1 : 1
2 : 1
3 : 1
4 : 1

Number : 7831432
1 : 1
2 : 1
3 : 2
4 : 1
7 : 1
8 : 1

Number : 5121112
1 : 4
2 : 2
5 : 1

Number : -4211221
1 : 3
2 : 3
4 : 1

Number : 1177714
1 : 3
4 : 1
7 : 3``````
``````#   Python 3 Program
#   Find the frequency of each digit in a number

class MyNumber :
# Find the occurrence of digits in number
def occurrence(self, number, result) :
temp = 0
while (number != 0) :
temp = number % 10
if (temp < 0) :
temp = -temp

result[temp] += 1
number = int(number / 10)

def frequency(self, number) :
# Set initial frequency of digit
result =  * 10
i = 0
print("\nNumber : ", number ,"", end = "")
if (number < 0) :
number = -number

self.occurrence(number, result)
i = 0
while (i < 10) :
if (result[i] != 0) :
print("\n ", i ," : ", result[i] ,"", end = "")

i += 1

print("\n", end = "")

def main() :
obj = MyNumber()
# Test Case
obj.frequency(1234)
obj.frequency(7831432)
obj.frequency(5121112)
obj.frequency(-4211221)
obj.frequency(1177714)

if __name__ == "__main__": main()``````

#### Output

``````Number :  1234
1  :  1
2  :  1
3  :  1
4  :  1

Number :  7831432
1  :  1
2  :  1
3  :  2
4  :  1
7  :  1
8  :  1

Number :  5121112
1  :  4
2  :  2
5  :  1

Number :  -4211221
1  :  3
2  :  3
4  :  1

Number :  1177714
1  :  3
4  :  1
7  :  3``````
``````#   Ruby Program
#   Find the frequency of each digit in a number

class MyNumber

# Find the occurrence of digits in number
def occurrence(number, result)

temp = 0
while (number != 0)

temp = number % 10
if (temp < 0)

temp = -temp
end
result[temp] += 1
number /= 10
end
end
def frequency(number)

# Set initial frequency of digit
result = Array.new(10) {0}
i = 0
print("\nNumber  : ", number ,"")
if (number < 0)

number = -number
end
self.occurrence(number, result)
i = 0
while (i < 10)

if (result[i] != 0)

print("\n ", i ,"  : ", result[i] ,"")
end
i += 1
end
print("\n")
end
end
def main()

obj = MyNumber.new()
# Test Case
obj.frequency(1234)
obj.frequency(7831432)
obj.frequency(5121112)
obj.frequency(-4211221)
obj.frequency(1177714)
end
main()``````

#### Output

``````Number  : 1234
1  : 1
2  : 1
3  : 1
4  : 1

Number  : 7831432
1  : 1
2  : 1
3  : 2
4  : 1
7  : 1
8  : 1

Number  : 5121112
1  : 4
2  : 2
5  : 1

Number  : -4211221
1  : 3
2  : 3
4  : 1

Number  : 1177714
1  : 3
4  : 1
7  : 3
``````
``````/*
Scala Program
Find the frequency of each digit in a number
*/
class MyNumber
{
//Find the occurrence of digits in number
def occurrence(num: Int, result: Array[Int]): Unit = {
var temp: Int = 0;
var number: Int = num;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
temp = -temp;
}
result(temp) += 1;
number = (number / 10).toInt;
}
}
def frequency(num: Int): Unit = {
//Set initial frequency of digit
var result: Array[Int] = Array.fill[Int](10)(0);
var i: Int = 0;
var number: Int = num;
print("\nNumber : " + number + "");
if (number < 0)
{
number = -number;
}
occurrence(number, result);
i = 0;
while (i < 10)
{
if (result(i) != 0)
{
print("\n " + i + " : " + result(i) + "");
}
i += 1;
}
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
//Test Case
obj.frequency(1234);
obj.frequency(7831432);
obj.frequency(5121112);
obj.frequency(-4211221);
obj.frequency(1177714);
}
}``````

#### Output

``````Number : 1234
1 : 1
2 : 1
3 : 1
4 : 1

Number : 7831432
1 : 1
2 : 1
3 : 2
4 : 1
7 : 1
8 : 1

Number : 5121112
1 : 4
2 : 2
5 : 1

Number : -4211221
1 : 3
2 : 3
4 : 1

Number : 1177714
1 : 3
4 : 1
7 : 3``````
``````/*
Swift Program
Find the frequency of each digit in a number
*/
class MyNumber
{
//Find the occurrence of digits in number
func occurrence(_ num:  Int, _ result: inout [Int])
{
var temp: Int = 0;
var number: Int = num;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
temp = -temp;
}
result[temp] += 1;
number /= 10;
}
}
func frequency(_ num: Int)
{
//Set initial frequency of digit
var result: [Int] = Array(repeating: 0, count: 10);
var i: Int = 0;
var number: Int = num;
print("\nNumber : ", number ,"", terminator: "");
if (number < 0)
{
number = -number;
}
self.occurrence(number, &result);
i = 0;
while (i < 10)
{
if (result[i] != 0)
{
print("\n ", i ," : ", result[i] ,"", terminator: "");
}
i += 1;
}
print("\n", terminator: "");
}
}
func main()
{
let obj: MyNumber = MyNumber();
//Test Case
obj.frequency(1234);
obj.frequency(7831432);
obj.frequency(5121112);
obj.frequency(-4211221);
obj.frequency(1177714);
}
main();``````

#### Output

``````Number :  1234
1  :  1
2  :  1
3  :  1
4  :  1

Number :  7831432
1  :  1
2  :  1
3  :  2
4  :  1
7  :  1
8  :  1

Number :  5121112
1  :  4
2  :  2
5  :  1

Number :  -4211221
1  :  3
2  :  3
4  :  1

Number :  1177714
1  :  3
4  :  1
7  :  3``````

## Resultant Output Explanation

The output shows the frequency of each digit in the given numbers.

• For the number 1234, all digits (1, 2, 3, and 4) occur only once in the number.
• For the number 7831432, the digits 1, 2, 3, 4, 7, and 8 have frequencies of 1, 1, 2, 1, 1, and 1, respectively.
• For the number 5121112, the digits 1, 2, and 5 have frequencies of 4, 2, and 1, respectively.
• For the number -4211221, the digits 1, 2, and 4 have frequencies of 3, 3, and 1, respectively.
• For the number 1177714, the digits 1, 4, and 7 have frequencies of 3, 1, and 3, respectively.

## Time Complexity

Let's analyze the time complexity of the algorithm:

The `occurrence` function iterates through each digit of the number once, and the time taken for this operation is proportional to the number of digits in the given number. The number of digits in a positive integer N is approximately log10(N). Thus, the time complexity of the algorithm is O(log(N)).

Keep in mind that this time complexity assumes that the number of digits is not excessively large. If the number of digits is bounded by a constant (which is often the case in practice), the algorithm's time complexity can be considered constant, O(1). However, for very large numbers with an unbounded number of digits, the time complexity remains logarithmic.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.