Python Program to Find the Frequency of a Digit in a Number

frequency-of-digit

Python programs to find the frequency of a digit in a number have been shown here. Frequency denotes the number of occurrences of an element. For example, if a number is 556622, the frequency of the digit 2 in that number is 2.

In the following section, the algorithm, pseudocode, and time complexity of the programs have also been covered.






1. Algorithm to find the frequency of a digit in a number


1. Take a number n and a digit d as inputs.

2. Initialize a counter c = 0.

3. If n = d = 0, set c = 1 and go to step 7.

4. Perform c = c + 1, if n mod 10 = d

5. Perform n = floor(n / 10)

6. Go to step 4, if n > 0, else go to step 7

7. Declare value of c as the output.




2. Pseudocode to find the frequency of a digit in a number


Input : A number $n$ and a digit $d$

Output : Frequency of $d$ in $n$

1. Procedure countFrequency($n$, $d$):

2. $c \leftarrow 0$

3. If $n = 0$ and $d = 0$:

4. $c \leftarrow 1$

5. Else:

6. Repeat until $n = 0$

7. If $n \mod 10 = d$:

8. $c \leftarrow c + 1$

9. $n \leftarrow \lfloor{\frac{n}{10}}\rfloor$

10. Return $c$

11. End Procedure





3. Time complexity to find the frequency of a digit in a number


Time Complexity: O(log(n))

Where n is the input number.





4. Program & Output to find the frequency of a digit in a number




4.1. Python Program & Output to find the frequency of a digit in a number Using Iteration

Code has been copied
#***********************************
#       alphabetacoder.com
# Python program to count frequency 
# of a digit in a number
#***********************************

# take input
num = int(input("Enter the integer = "))
d = int(input("Enter the digit = "))

# copy
t = num

#initialize
count = 0

# count frequency of digit d in the number num
if num == 0 and d == 0:
    count += 1

while num >0:
    # find unit digit of current number
    r = num % 10
    # Increase the counter if unit digit = target digit
    if r == d:
        count += 1

    num = num // 10

# display the result
print("Frequency of", d, "in", t, "=",count)

Output


Case 1:

Enter the integer = 55224455

Enter the digit = 5

Frequency of 5 in 55224455 = 4


Case 2:

Enter the integer = 12451122

Enter the digit = 6

Frequency of 6 in 12451122 = 0



This Python program is designed to count the frequency of a specific digit within an integer. Here's an explanation of the code:

  • First, it takes input from the user for two values:
    num: The integer in which you want to count the frequency of a digit.
    d: The digit that you want to count within the integer.
  • It makes a copy of the original number and stores it in a variable t. This is done to ensure that the original number is preserved for displaying the result later.
  • It initializes a variable count to 0. This variable will be used to keep track of the frequency of the digit d in the number num.
  • The code checks a special case when num is 0 and d is 0. If both are 0, it means that the only digit in the number is 0, and in this case, it increments the count variable by 1.
  • The program then enters a while loop that continues as long as the value of num is greater than 0. This loop will iterate through each digit of the number from right to left.
  • Inside the loop, it extracts the rightmost digit of num by taking the remainder of num when divided by 10. This digit is stored in a variable r.
  • It checks if the extracted digit r is equal to the target digit d. If they are equal, it increments the count variable by 1, indicating that the digit d was found.
  • The code then updates the value of num by integer division (//) by 10. This effectively removes the rightmost digit, and the loop continues with the next digit to the left.
  • The loop continues until all digits of the original number have been examined.
  • After the loop completes, it prints the result, displaying the frequency of the digit d in the original number t.

  • So, the program reads an integer and a digit, counts how many times that digit appears in the integer, and then prints the result.


4.2. Python Program & output to find the frequency of a digit in a number using recursion

Code has been copied
#**************************************
#           alphabetacoder.com
# Python program to count frequency of 
# a digit in a number using recursion
#**************************************

# recursive function to count frequency
# of a digit in an integer
def count_frequency(num, d):
    # exit condition
    if num == 0:
        return 0
    # call the function
    return (num % 10 == d) + count_frequency(num // 10, d)

def main():
    # take input
    num = int(input("Enter the integer = "))
    d = int(input("Enter the digit = "))

    # count frequency of digit d in the number num
    if num == 0 and d == 0:
        count = 1
    else:
        # count the frequency of the digit in a 
        # number by calling recursive function
        count = count_frequency(num, d)

    # display the result
    print("Frequency of", d, "in", num, "=",count)

# driver code
main()

Output


Enter the integer = 55884411

Enter the digit = 8

Frequency of 8 in 55884411 = 2



    This Python program counts the frequency of a specific digit within an integer using a recursive function. Here's an explanation of the code:

  • The code defines a recursive function count_frequency that takes two parameters:
    num: The integer in which you want to count the frequency of a digit.
    d: The digit that you want to count within the integer.
  • Inside the count_frequency function, there is an exit condition that checks if num is equal to 0. If num is 0, it means there are no more digits to examine, so the function returns 0, indicating that the digit d was not found.
  • If num is not equal to 0, the function proceeds with the recursion. It checks whether the rightmost digit of num (obtained using num % 10) is equal to the target digit d. If they are equal, it returns 1, indicating that the digit d was found in this position.
  • The function then makes a recursive call to itself, passing the remaining part of the number (num // 10) and the same target digit d.
  • The results of the recursive calls are accumulated as they return, and this process continues until all digits in the number have been examined. The function returns the total count of occurrences of the digit d in the number.
  • The main function is defined, where the user is prompted to input two values:
    num: The integer in which you want to count the frequency of a digit.
    d: The digit that you want to count within the integer.
  • There's a special case check when both num and d are 0. If both are 0, it means that the only digit in the number is 0, and in this case, count is set to 1.
  • If the above condition is not met, the code calls the count_frequency function, passing num and d as arguments to calculate the frequency of the digit d in the number.
  • Finally, the program prints the result, displaying the frequency of the digit d in the original number num.
  • The main function is called at the end, which in turn initiates the entire program by taking user input and displaying the result.