Unique Digits Count in Java

Unique Digits Count in Java | Write a Java program to count the unique digits in the given number. For example, if the given number is 01230312 then the total number of unique digits is 4.

To develop this Java program, we have to take the help of array or collection. First, we develop this program using Collection, and then we will do the same using the normal Array.

Take a number and using loop iterate each digit of the number. Check each digit and store into an array or Collection.

A number can have 0 to 9 unique digits so, we can’t predict the accurate number of unique digits. Therefore, we have to use a Set collection instead of Array. An array has a fixed length but Collections are dynamic growable.

In this collection, we have to store only unique elements. For example, the given number can contain the digit ‘1’ multiple times but have to count it only once. So, we can use HashSet Collection which stores only unique elements.

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class UniqueDigits{

   public static void main(String[] args) {

      // declare variables
      Scanner scan = null;
      int num = 0;

      // create Scanner object
      scan = new Scanner(System.in);

      // read input
      System.out.print("Enter an Integer:: ");
      num = scan.nextInt();

      // display size of collection
      System.out.println("Unique digits = " + 
                   countUniqueDigits(num));

      // close Scanner object
      scan.close();
  }

  public static int countUniqueDigits(int num) {
      Set<Integer> digits = new HashSet<Integer>();

      // iterate to each digit
      while(num > 0) {
         // find last digit
         // add it to collection
         digits.add(num % 10);
         // remove last digit
         num /= 10;
      }

      // return size
      return digits.size();
  }

}

Enter an Integer:: 12300123
Unique digits = 4


Using Array

Here, we will take an int array of length 10 because any number can’t have more than 10 unique digits i.e. 0-9. Initialize this array with 0s. Check each digit of the number. Number%10 gives the last digit of the number and number/10 removes the last digit of the number. If the digit is N (digit = number % 10) then update array[N] = 1. Similarly, check each digit, and finally count how many 1s are available in the array, it is the unique digits in the given number.

Steps to Develop a Java method to count unique digits in a given number,
1) Take a number.
2) Declare an array of length 10, because any number can’t have more than 10 unique digits i.e. 0-9.
3) Initialize this array with 0’s.
4) Take a remainder variable and count variable. Initialize count variable with 0.
5) Find the last digit of the number
6) Update array[last_digit] = 1
7) Remove the last digit from the number
8) Repeat 5 to 7 steps until the number becomes 0
9) Finally, count how many 1’s are available in the array, and return this value.

Java Program to count the unique digits in a given number

package com.know.java;

import java.util.Scanner;

public class UniqueDigit {

   // method to count unique digits
   private static int countUniqueDigit(long num) {

      // declare variables
      int arr[] = new int[10];
      int remainder = 0;
      int count = 0;
		
      // initialize array values with 0
      for(int i=0; i<arr.length; i++) {
         arr[i] = 0;
      }
		
      // check each digit of number
      while(num > 0) {
         // find last digit
         remainder = (int)(num % 10);

         // update array values
         arr[remainder] = 1;

         // remove last digit
         num /= 10;			
      }
		
      // check with array values
      for(int i: arr) {
         if(i == 1) count++;
      }
		
      return count;
   }

   // main method
   public static void main(String[] args) {

      // declare variables
      Scanner scan = null;
      long num = 0;
      int uniqueDigit = 0;

      // create Scanner class object
      scan = new Scanner(System.in);

      // take input
      System.out.print("Enter an Integer number:: ");
      num = scan.nextLong();

      // call countUniqueDigit() method
      uniqueDigit = countUniqueDigit(num);

      // display result
      System.out.println("Unique digits = "+ uniqueDigit);
	
      // close Scanner object
      scan.close();		
   }

}

Output for different test:-

Enter an Integer number:: 564222546
Unique digits = 4

Enter an Integer number:: 01230123590
Unique digits = 6


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Similar Java programming examples

Leave a Reply