Unique Number in Java

In this post, we will develop a Java program to check whether the given number is a unique number or not.

A number that doesn’t have any duplicate digits is called a unique number. For example:- 123, 7158, 98547 are unique numbers, but 1111, 1233, 514425 are not unique numbers because they have one digit more than once.

For this task we need to use 2 loops (inner loop and outer loops). We have multiple ways to develop a Java method to check whether the given number is unique number or not.
1) By comparing each digits manually
2) Using Array
2) Using String

First, we will compare each digit manually. Procedure:-
1) Take a number
2) Find its last digit.
3) Compare all digits of the number with this last digit.
4) If it found more than one time then the number is not a unique number.
5) Else remove the last digit of the number
6) Repeat the 2 to 5 steps until the number becomes zero.

import java.util.Scanner;

public class UniqueNumber {

   // method to check the Unique number
   public static boolean isUnique(int number) {

      int n1 = number; //1st temporary variable
      int lastDigit = 0;
      int n2; //2nd temporary variable
      int count; 

      // iterate through all digits of number
      while(n1 != 0) {

         // find last digits
         lastDigit = n1 % 10;

         // compare this digit with 
         // all digits
         n2 = number;
         count = 0;

         // loop
         while(n2 != 0) {

            // find last digit
            // and compare
            if(lastDigit == n2%10)
            count++;

            // remove last digit
            n2 /= 10;
         }

         if(count != 1)
         return false;

         // remove last digit
         n1 /= 10;
      }

      return true;
   }

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

      // declare variables
      int number = 0;
      boolean result = false;

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

      // take input from end-user
      System.out.print("Enter an integer number::");
      number = scan.nextInt();

      // check number is Unique number or not
      result = isUnique(number);

      // display
      if(result)
      System.out.println(number +
                 " is a Unique number.");
      else
      System.out.println(number +
                 " is not a Unique number.");

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

The output for the different test-cases are:-

Enter an integer number::1234
1234 is a Unique number.

Enter an integer number::456845
456845 is not a Unique number.

Check Unique number using Array

Now, We will develop the same program using an array. The idea is to find all digits of the number and store it in the array. Later compare values of all indexes with each other if they are the same the number is not a unique number.

But before comparing all indexes we need to declare an array of size “number_of_digits in the number”.

import java.util.Scanner;

public class UniqueNumberUsingArray {

   // Method to check the Unique number
   public static boolean isUnique(int number) {

      // count number of digits
      int digits = countDigits(number);

      // declare array
      int[] arr = new int[digits];

      // initialize array
      for(int i=0; i<digits; i++) {

         // add last digit to ith position
         arr[i] = (int)number%10;

         // remove last digit
         number /= 10;
      }

      // compare array elements
      for(int i=0; i<digits; i++) {
         for(int j=0; j<digits; j++) {
            if(i!=j && arr[i]==arr[j])
            return false;
         }
      }

      return true;
   }

   public static int countDigits(int number) {
      int count = 0;
      while(number != 0) {
         count++;
         number /= 10;
      }
      return count;
   }

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

      // declare variables
      int number = 0;
      boolean result = false;

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

      // take input from end-user
      System.out.print("Enter an integer number::");
      number = scan.nextInt();

      // check number is Unique number or not
      result = isUnique(number);

      if(result)
      System.out.println(number +
                  " is an Unique number.");
      else
      System.out.println(number +
                 " is not an Unique number.");

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

Using String

First, convert the given number into a String and then compare every character of the String with each other.

import java.util.Scanner;

public class UniqueNumberUsingString {

   // Method to check the Unique number
   private static boolean isUnique(int number) {

      // convert number to string
      String str = Integer.toString(number);

      // compare characters of the String
      for(int i=0; i<str.length(); i++) {
         for(int j=0; j<str.length(); j++) {
            if( i != j   && 
                str.charAt(i)==str.charAt(j) )
               return false;
         }
      }

      return true;
   }

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

      // declare variables
      int number = 0;
      boolean result = false;

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

      // take input from end-user
      System.out.print("Enter an integer number::");
      number = scan.nextInt();

      // check number is Unique number or not
      result = isUnique(number);

      // display result
      if(result)
      System.out.println(number +
                  " is an Unique number.");
      else
      System.out.println(number +
                 " is not an Unique number.");

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

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

1 thought on “Unique Number in Java”

  1. Thanks for taking to time to discuss this, I feel strongly about it and adore finding out additional on this topic. If feasible, as you acquire expertise, would you thoughts updating your blog with much more details? It is extremely useful for me.

Leave a Comment

Your email address will not be published. Required fields are marked *