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

frequency-of-digit

Java 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 11223344, the frequency of the digit 3 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. Java Program & Output to find the frequency of a digit in a number Using Iteration

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

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        int num, d, r, t, count = 0;

        // take inputs
        System.out.print("Enter the integer = ");
        num = sc.nextInt();
        System.out.print("Enter the digit = ");
        d = sc.nextInt();

        // copy
        t = num;

        //count the frequency of the digit
        if (num == 0 && d == 0) {
            count++;
        }
        while (num > 0) {
            // find unit digit of current number
            r = num % 10;
            // Increase the counter if unit digit = target digit
            if (r == d)
                count++;

            num = num / 10;
        }

        // display result
        System.out.print("Frequency of " + d + " in " + t + " = " + count);
    }
}

Output


Enter the integer = 12321

Enter the digit = 2

Frequency of 2 in 12321 = 2





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

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

import java.util.Scanner;

class Main {
    // recursive function to count frequency
    // of a digit in an integer
    public int count_frequency(int num, int d) {
        // exit condition
        if (num == 0)
            return 0;
        // call the function
        return ((num % 10 == d) ? 1 : 0) + count_frequency(num / 10, d);
    }

    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare object of class Main
        Main obj = new Main();

        // declare variables
        int num, d, count = 0;

        // take inputs
        System.out.print("Enter the integer = ");
        num = sc.nextInt();
        System.out.print("Enter the digit = ");
        d = sc.nextInt();

        // if both inputs are zero
        if (num == 0 && d == 0)
            count = 1;
        else {
            //count the frequency of the digit in a 
            //number by calling recursive function
            count = obj.count_frequency(num, d);
        }

        // display result
        System.out.print("Frequency of " + d + " in " + num + " = " + count);
    }
}

Output


Enter the integer = 668826

Enter the digit = 6

Frequency of 6 in 668826 = 3