Java Program to count frequency of a digit | Ask The Code

Updated: Mar 9

Program to count frequency of a digit using Java | Java Programming | AskTheCode

Problem:

Count the frequency of a particular digit in a given number.


Input sample:

3 983653

Output Sample:

2


Code:

import java.util.Scanner;
 
public class freq_of_a_digit{
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		int digit = scan.nextInt();
		int n = scan.nextInt();
		
		int[] arr = new int[20];
		int i = 0, freq = 0;
		while(n != 0){
			int val = n % 10;
			arr[i] = val;
			i++;
			n = n / 10;
		}
		for (int j = 0; j <= i; j++) {
			if (arr[j] == digit) {
				freq += 1;
			}
		}
		System.out.println(freq);
	}
}
35 views0 comments

Recent Posts

See All

Golf CodeChef Solution in Java - AskTheCode

CodeChef May Long Challenge Solution | Golf (LKDNGOLF) solution | AskTheCode Golf (LKDNGOLF) solution Problem: It's a lockdown. You’re bored in your house and are playing golf in the hallway. The hall

Solubility - CodeChef Solution in Java and C++| AskTheCode

CodeChef May Long Challenge Solution | Solubility (SOLBLTY) solution | AskTheCode Solubility (SOLBLTY) solution Problem: Suppose for a unit rise in temperature, the solubility of sugar in water increa

Valid Paths CodeChef Solution in C++ | AskTheCode

CodeChef May Long Challenge Solution | Valid Paths (VPATH) solution | AskTheCode Valid Paths (VPATH) CodeChef solution Problem: You are given a tree with N nodes numbered from 1 to N. A set S of nodes