
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a Number is Unique in Java
What is Unique Number?
A Unique number is a number that does not have a single repeated digit in it. In other words, a number in which all the digits are present exactly only one time.
For example, consider the number 12. The digits 1 and 2 each appear only once, the number is unique as it contains all distinct digits.
Here are some other examples of unique numbers such as 132, 1, 45, 98, 701, 5, 12, 1234, etc.
Input & Output Scenarios
Below are the few of the input and output scenarios which gives some idea about the unique element:
Scenario 1
Suppose the given number is 145:
Input: 145 Output: Yes
Since all the digits 1, 4, and 5 in number 145 are unique, so 145 is a unique number.
Scenario 2
Suppose we have the number 377:
Input: 377 Output: No
The number 377 contains the same number 7 two times, the number 377 is not unique.
Here are the different ways to check whether a number is unique:
- Checking Uniqueness of a Number by Comparing Digits
- Using toString() and charAt() Method
- Using HashSet Class
Checking Uniqueness of a Number by Comparing Digits
To determine if a number has unique digits, you can compare each digit with the others. To do so, extract each digit and manually compare it to the next one. If any digits are found repetitive, then the number does not have unique digits.
Example
In the example below, we extract each digit from the given number 123 and compare them with one another. If any digit is found as duplicate, the number is considered not unique:
public class checkUnique{ public static void main(String[] args){ int num = 123; System.out.println("The given number is: " + num); int copyOfOriginalNumber = num; int count = 0; while (num > 0){ //get rightmost digit int digit = num % 10; num = num / 10; int temp = num; while (temp > 0){ //if rightmost digit matches with any leftmost digit then stop loop (break) if (temp % 10 == digit){ count = 1; break; } temp = temp / 10; } } if (count == 0){ System.out.println("Yes! " + copyOfOriginalNumber + " is a unique number"); } else { System.out.println("No! " + copyOfOriginalNumber + " is not a unique number"); } } }
Following is the output of the above program:
Given number: 123 123 is a unique number
Using toString() and charAt() Method
Both toString() and charAt() are the String class methods. The toString() method retrieves the string representation of a given number, while the charAt() method accesses individual characters in a string.
As the charAt() method accesses an individual element, it will compare the current element with the next element to check whether they are equal or not.
Example
The program retrieves the string representation of the number "7890" and uses the charAt() method inside a nested loop to compare each element. While comparing if any element is found equal, it will be considered that the element is not unique:
public class checkUnique{ public static void main(String[] args){ int num = 7890; System.out.println("The given number is: " + num); int count = 0; //convert integer to String String str = Integer.toString(num); for(int i = 0;i < str.length()-1; i++){ for(int j = i+1; j < str.length(); j++){ //compare each element once find duplicate break the loop and exit if(str.charAt(i) == str.charAt(j)){ count++; break; } } } if (count == 0){ System.out.println("Yes! " + num + " is a unique number"); } else { System.out.println("No! " + num + " is not a unique number"); } } }
The above program produces the following output:
The given number is: 7890 Yes! 7890 is a unique number
Using the HashSet Class
In Java, HashSet is an unordered collection from the java.util package, used to store unique elements of the same type.
We store all the unique digits in a HashSet and count the number of digits in a number. We compare the size() of the HashSet with the number of digits present in the number. If both are equal, the number is a unique number; otherwise not.
import java.util.HashSet; public class checkUnique{ public static void main(String[] args){ int num = 1441; System.out.println("The given number is: " + num); int copy = num; HashSet<Integer> set = new HashSet<>(); int count = 0; while(num !=0){ int last_dig = num % 10; if(!set.contains(last_dig)){ set.add(last_dig); } count++; num = num / 10; } if(count == set.size()){ System.out.println("Yes! " + copy + " is a unique number"); } else{ System.out.println("No! " + copy + " is not a unique number"); } } }
Below is the output of the above program:
The given number is: 1441 No! 1441 is not a unqiue number.