Unique Number in Java Program29 Mar 2025 | 4 min read The number will be unique if it is positive integer and there are no repeated digits in the number. In other words, a number is said to be unique if and only if the digits are not duplicate. For example, 20, 56, 9863, 145, etc. are the unique numbers while 33, 121, 900, 1010, etc. are not unique numbers. In this section, we will create Java programs to check whether the number is unique or not. ![]() There are the following ways to check the number is unique or not: By Comparing Each Digit ManuallyThere are the following steps to check number is unique or not:
UniqueNumberExample1.java Output 1: Enter the number you want to check: 13895 The number is unique. Output 2: Enter the number you want to check: 11100 The number is not unique. Output 3: Enter the number you want to check: 10000 The number is not unique. Using StringUsing String, we can also check the number is unique or not. We use the charAt() method of the String to compare each digit of the string. UniqueNumberExample2.java Output 1: Enter the number you want to check: 9876 The number is unique. Output 2: Enter the number you want to check: 1010 The number is not unique. Output 3: Enter the number you want to check: 200 The number is not unique. In the above program first, we convert the number variable into String by using the toString() method. After that, we determine the length of the string using the length() method. We have used two for loop (inner and outer) that iterate over the number. Inside the if statement, for each iteration, we have compared the digits by using the charAt() method. If both digits are the same, the break statement breaks the execution of the program and prints the number is not unique, else prints the number is unique. Using ArrayWe can also use an array to check the number is unique or not. In this method, we find all the digits of the number and store it into an array. After that, compare the values of all the indexes with each other. If the values are the same, the number is not unique. Remember that before comparing all the indexes we required to declare the size of the array (number of digits). UniqueNumberExample3.java Output 1: Enter the number you want to check: 898 898 is not a unique number. Output 2: Enter the number you want to check: 201 201 is a unique number. Output 3: Enter the number you want to check: 700 700 is not a unique number. |
In this section, we will learn what is a fascinating number and also create Java programs to check if the given number is fascinating or not. The fascinating number program is frequently asked in Java coding tests. Fascinating Numbers Multiplying a number by two and three separately, the...
3 min read
A string inStr is given to us. The task is to compute the total number of unique substrings of the string inStr. All the characters of the input string are lowercase alphabets. Example 1: Input String inStr = "abcde" Output: There are 16 unique substrings. Explanation: The distinct substrings are: "",...
6 min read
In Java, while we deal with date and time, sometimes we need to compare dates. The comparison of dates in Java is not the same as the comparison of two numbers. So, it is a little bit tricky task to compare two dates in Java. We...
6 min read
In Java, the Object class is the parent class of all the Java classes. Every Java class is a direct or indirect child of the Java Object class. Hence, every Java class extends the Object class. Therefore, we need not to write the following statement...
3 min read
Java, a versatile and widely used programming language, is renowned for its object-oriented approach and platform independence. One of the key features contributing to the language's organizational prowess is the concept of packages. In Java, packages serve as containers for classes, providing a structured way to...
6 min read
In Java, public and private are keywords that are known as an access modifier or specifier. It restricts the scope or accessibility of a class, constructor, variables, methods, and data members. It depends on which it is applied. Java provides the four types of access...
6 min read
The java.time.format.DecimalStyle class toString() method. To obtain this DecimalStyle's String value in Java, utilize the DecimalStyle class. The String value is represented by a String that is returned by this function. Syntax: public String toString() Parameter: No parameters are taken by this main method. Return Value: The...
2 min read
Java is a versatile programming language, known for its rich library of classes and methods that empower developers to create complex and interactive graphical user interfaces (GUIs). When it comes to creating GUI components in Java, the setBounds() method plays a vital role. In this section,...
4 min read
Amazon Web Services (AWS) offers many services that enable businesses to develop, deploy, and manage applications and infrastructure in the cloud. It is important to monitor these resources to ensure they are operating reliably and efficiently. AWS CloudWatch is a monitoring service that collects and tracks...
5 min read
How to Initialize an Array in Java? Java initialize array is basically a term used for initializing an array in Java. We know that an array is a collection of similar types of data. The array is a very important data structure used for solving programming...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India