How to Write a java program to find a unique number in java. program in java that accepts an integer as input and finds whether the number is Unique or not. How to find a Unique number in java.
Before diving into code Let’s understand what is a unique number. Then will see how to write the java code to find a unique number Print Unique if the number is “Unique”, else print “Not Unique”.
What is a unique number?
A Unique number is a positive integer (without leading zeros) with no duplicate digits. For example 7, 135, 214 are all unique numbers whereas 33, 3121, 300 are not unique numbers.
Sample Input and Output to check a unique number in Java
- Input consists of an integer.
- The output consists of a String (“Unique” or “Not Unique”).
- Refer to sample output for formatting specifications.
Sample Input 1:
123
Sample Output 1:
Unique
Sample Input 2:
33
Sample Output 2:
Not Unique
Here, we will see some possible ways where we can find a unique number in Java.
How to check whether a number is a unique number or not in java?
We can find the number is unique or not by using the following ways:
- By using Arrays
- By using String
- By comparing each digit.
Java program to Check Unique number or not
Following are the steps to check whether a number is a unique number or not:
- Take a number from the user.
- Store the number into temporary variables i.e temp1 and temp2.
- Search the last digit of the number.
- Now, compare each digit with the last digit.
- If the digit is repeated, then the print number is not unique.
- Otherwise, print a unique number.
- repeat the same process till the temporary variable value is greater than 0.
Java Code to find Unique number
package com.company; import java.util.Scanner; public class CheckUnique { public static void main(String[] args) { int number,rem1,rem2,count = 0; System.out.println("Enter number: "); Scanner scanner = new Scanner(System.in); number = scanner.nextInt(); int temp1 = number; int temp2 = number; while(temp1 > 0){ rem1 = temp1 % 10; while(temp2 > 0){ rem2 = temp2 % 10; if(rem1 == rem2){ count++; } temp2 = temp2 / 10; } temp1 = temp1 / 10; } if(count == 1){ System.out.println(+number+" is Unique."); } else{ System.out.println(+number+" is not Unique"); } } }
Output
Java Program to find the unique number in Array
Now, we will see another way to find unique numbers using an Array. Following are the steps we will follow
- Take the number as input.
- Create an empty array of integers. Initialize count variable.
- Find the last digit and add it to the ith position of the array.
- Now, compare the elements in an array
- If the number is the same, increment the counter.
- At last, check the value of the count variable. If the count value is greater than 0 then print non-unique
package com.company; import java.util.Scanner; public class Hello { public static void main(String[] args) { System.out.println("Enter number"); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[100]; int i = 0, count = 0; while (n != 0) { int num = n % 10; a[i] = num; i++; n = n / 10; } for (int j = 0; j <= i - 1; j++) { for (int k = j + 1; k <= i - 1; k++) { if (a[j] == a[k]) { count++; } } } if (count > 0) { System.out.println("Not Unique"); } else { System.out.println("Unique"); } } }
Output
Java Program to find the unique number using String
Now, we will see another way to find unique numbers using a String. Following are the steps we will follow
- Take a number in String as an input and set the flag value to false.
- Now, iterate over the string to check the repetition of digits.
- use charAt() method and compare each digits.
- If the digits are repeated set the flag to true.
- At last, check the flag value. If the flag value is false then print the number is unique or else print the number is not unique.
package com.company; import java.util.Scanner; public class UniqueNumber { public static void main(String[] args) { String number; boolean flag = false; System.out.println("Enter number :"); Scanner scanner = new Scanner(System.in); number = scanner.next(); for (int i = 0; i < number.length(); i++) { for (int j = i + 1; j < number.length(); j++) { if (number.charAt(i) == number.charAt(j)) { flag = true; break; } } } if (flag == false) { System.out.println(number + " is unique number"); } else { System.out.println(number + " is not unique number"); } } }
Output
Thus, this is how we can find a unique number in Java with the help of String, Array, and by comparing.