
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How To Check Whether a Number is a Unique Number or Not in Java?
Unique number can be defined as a number which does not have a single repeated digit in it. Simply it means all the digits of that number are unique digits, no duplicate digits are there in that number.
In this article we will see how to check whether a number is a unique number or not by using Java programming language.
To show you some instances
Instance-1
Input number is 145
Let’s check it by using the logic of unique number −
The digits are 1, 4, 5, all are unique digits.
Hence, 145 is a unique number.
Instance-2
Input number is 37
Let’s check it by using the logic of unique number −
The digits are 3, 7, all are unique digits.
Hence, 37 is a unique number.
Instance-3
Input number is 377
Let’s check it by using the logic of unique number −
The digits are 3, 7, 7 all are not unique digits.
Hence, 377 is not a unique number.
Some other examples of unique numbers include 132, 1, 45, 98, 701, 5, 12, 1234 etc.
Algorithm
Algorithm 1
Step 1 − Get an integer number either by initialization or by user input.
Step 2 − Take an outer while loop and get the rightmost digit.
Step 3 − Take another inner while loop to compare the rightmost digit of step-1 with the leftmost digits. If at any point digits match, the given number has repeated digits. Hence print the given number is not unique and repeat step-2 and step-3 till step-2 covers each digit of the number.
Step 4 − At last, if no matches are found then print the given number is unique.
Algorithm 2
Step 1 − Get an integer number either by initialization or by user input.
Step 2 − Take one outer for loop to iterate each digit one by one.
Step 3 − Take an inner for loop and compare the leftmost digit of outer for loop with all rightmost digits of inner loop. If at any point digits matches means the given number has repeated digits. Hence print the given number is not unique and repeat step-2 and step-3 till step-2 covers each digit of the number.
Step 4 − At last, if no matches are found then print the given number is unique.
Multiple Approaches
We have provided the solution in different approaches.
By Comparing Each Digit Manually
By Using User String
Let’s see the Java program along with its output one by one.
Approach-1: By Comparing Each Digit Manually
In this approach one integer value will be initialized in the program and then by using the Algorithm-1 we can check whether a number is a unique number or not.
Example
public class Main{ //main method public static void main(String[] args){ //Declared an integer variable and initialized a number as value int originalNumber = 123; //printing the given number to be checked System.out.println("Given number: "+originalNumber); //keep a copy of original number int copyOfOriginalNumber = originalNumber; //initializing count as 1 int count =0; //compare each digit with others //we will start comparing from rightmost digit with leftmost digits while (originalNumber > 0){ //get the rightmost digit int digit = originalNumber % 10; //remove the rightmost digit and get the updated number originalNumber = originalNumber / 10; //assign the updated original number(rightmost digit removed) to a temp variable int temp = originalNumber; //check if the rightmost digit matches with leftmost digits while (temp > 0){ //if rightmost digit matches with any leftmost digit then //increment count value //and break if (temp % 10 == digit){ count = 1; break; } //remove rightmost digit from temp //and get updated temp value temp = temp / 10; } } //if count value is 1 then print given number is unique number if (count == 0){ System.out.println(copyOfOriginalNumber+" is a unique number"); } //else print given number is not a unique number else { System.out.println(copyOfOriginalNumber+" is not a unique number"); } } }
Output
Given number: 123 123 is a unique number
Approach-2: By Using String
In this approach one integer value will be initialized in the program and then by using the Algorithm-2 we can check whether a number is a Peterson number or not.
Example
public class Main{ //main method public static void main(String[] args){ //Declared an integer variable and initialized a number as value int originalNumber = 7890; //printing the given number to be checked System.out.println("Given number: "+originalNumber); //initialize count as 0 int count = 0; //convert integer to String by using toString() method String st = Integer.toString(originalNumber); //Find length of String by using length() method int length=st.length(); //check if the leftmost digit matches with rightmost digits for(int i=0;i<length-1;i++){ for(int j=i+1;j<length;j++){ //if matches then increment count and break if(st.charAt(i)==st.charAt(j)){ count++; break; } } } //if count value is 1 then print given number is unique number if (count == 0){ System.out.println(originalNumber+" is a unique number"); } //else print given number is not a unique number else { System.out.println(originalNumber+" is not a unique number"); } } }
Output
Given number: 7890 7890 is a unique number
In this article, we explored how to check a number whether it is a unique number or not in Java by using different approaches.
- Related Articles
- How to Check Whether a Number is Krishnamurthy Number or Not in Java?
- How To Check Whether a Number is Strontio Number or Not in Java?
- How To Check Whether a Number is Tcefrep Number or Not in Java?
- How To Check Whether a Number is Tech Number or Not in Java?
- How To Check Whether a Number Is a Pronic Number or Not in Java?
- How To Check Whether a Number Is a Bouncy Number or Not in Java?
- How To Check Whether a Number is a Evil Number or Not in Java?
- How To Check Whether a Number Is a Fascinating Number or Not in Java?
- How To Check Whether a Number is a Harshad Number or Not in Java?
- How To Check Whether a Number Is a Insolite Number or Not in Java?
- How To Check Whether a Number is a Keith Number or Not in Java?
- How To Check Whether a Number Is a Niven Number or Not in Java?
- How To Check Whether a Number is a Sunny Number or Not in Java?
- How to Check Whether a Number is a Triangular Number or Not in Java?
- How To Check Whether a Number is a Goldbach Number or Not in Java?
