Find frequency of digits in a number in java
This problem is pretty common in interviews and computer examinations. Candidate is required to write a program where a user enters any random integer and the program should display the digits and their count in the number.
For Example, if the entered number is 74526429492, then frequency of 7 is 1, 4 is 3, 5 is 1, 6 is 1, 2 is 3, 9 is 2.
In tabular format,

NumberFrequency
71
43
51
61
23
92

There can be many ways to program the above problem. This article will explain two ways to find frequency of each digit in a number with explanation and output

Method 1 : Using array
Initialize an array of size 10 whose each location represents a digit from 0 to 9. Value at a location of the array is the count(or frequency of occurrence) of that digit.That is, a value of 2 at index 5 means that 5 occurs 2 times in the number.

Extract the digits of the number by taking modulus of the number by 10. Increment the count at location corresponding to the digit extracted.
Thus, if the digit is 2, then increment the value at 2nd position of the array, if the digit is 8, then increment the value at 8th position of the array and so on.

Continue this till all the digits of the array have been covered.

public static void main(String[] args) { 
  Scanner scanner = new Scanner(System.in); 
  System.out.println("Enter the number"); 
  int number = scanner.nextInt(); 
  // initialize array to hold count of digits 
  int[] digitArray = new int[10]; 
  int remainder = 0; 
  // loop to get digits of the number 
  while (number > 0) { 
    // get the last digit 
    remainder = number % 10; 
    // increment the count of this digit 
    digitArray[remainder]++; 
    // remove the last digit from the number 
    number = number / 10; 
  } 
  System.out.println("-------------------------"); 
  System.out.println("Number\tFrequency"); 
  System.out.println("-------------------------"); 
  // iterate over the digit array. Each count represents a digit from 0 to 9 
  for (int counter = 0; counter < digitArray.length; counter++) { 
    // get the count 
    int digitCount = digitArray[counter]; 
    if (digitCount != 0) { 
      System.out.println(counter + "\t" + digitCount); 
    } 
  } 
  scanner.close(); 
}

Output

Enter the number
2345342
———————
Number   Frequency
———————
2              2
3              2
4              2
5              1

Method 2 : Using java.util.Map

Initialize a java.util.Map which will hold the digit as a key and its frequency of occurrence in the number as value.
As in the above example, each digit is extracted and every time it is put as a key, the value corresponding to the key is incremented.
In order the print the digits and their frequency count, the map is iterated.

public static void main(String[] args) { 
  Scanner scanner = new Scanner(System.in); 
  System.out.println("Enter the number"); 
  int number = scanner.nextInt(); 
  // initialize map to hold digits and their count 
  Map<Integer, Integer> digitMap = new HashMap<Integer, Integer>(); 
  int remainder = 0; 
  // loop to get digits of the number 
  while (number > 0) { 
    // get the last digit 
    remainder = number % 10; 
    // get the frequency count of this digit 
    Integer count = digitMap.get(remainder); 
    // for the first time count will be null, hence put 1 
    if (count == null) { 
      digitMap.put(remainder, 1); 
    } else { 
      // increment the count for this digit 
      digitMap.put(remainder, ++count); 
    } 
    // remove the last digit from the number 
    number = number / 10; 
  } 
  System.out.println("-------------------------"); 
  System.out.println("Number\tFrequency"); 
  System.out.println("-------------------------"); 
  Iterator iterator = digitMap.keySet().iterator(); 
  while (iterator.hasNext()) { 
    int digit = iterator.next(); 
    int digitCount = digitMap.get(digit); 
    System.out.println(digit + "\t" + digitCount); 
  } 
  scanner.close(); 
}

Output

Enter the number
2345342
————————-
Number   Frequency
————————-
2              2
3              2
4              2
5              1

Let’s tweak in :

  1. A java.util.Map stores values in key-value pairs. The keys are unique and when a value with an already existing key is inserted, it overwrites the previous value corresponding to that key.

Hope this post was helpful.