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. |
Arrays in Java are basic data structures used to store and manipulate collections of objects of the same type. The limitation of arrays in Java, however, is that they inherently cannot store objects. This limitation can be overcome by using a normal setting. Java introduced generics...
4 min read
In this section, we will discuss the Java modulo operator. Operator In mathematics, there is basically four arithmetic operators addition (+), subtraction (-), multiplication (*), and division (/). In programming, except for these four operators, there is another operator called modulo or modulus operator. It is represented...
2 min read
Just like the Red-Black Tree, the AVL tree is another self-balancing BST(Binary Search Tree) in Java. In the AVL tree, the difference between heights of the right and left subtree doesn't exceed one for all nodes. It takes O(h) time to perform the search, max, min,...
6 min read
Boyer-Moore algorithm is a string searching or matching algorithm developed by Robert S. Boyer and J Strother Moore in 1977. It is a widely used and the most efficient string-matching algorithm. It is much faster than the brute-force algorithm. In this section, we will discuss the...
12 min read
Problem Statement Write a Java program that determines whether a grid number sequence forms a geometric progression (GP). A geometric progression is defined as a sequence in which each joint after the first joint is obtained by multiplying the preceding joint by a constant. The program should: ...
6 min read
Interface Variable in Java Java interfaces provide a way to define a contract or blueprint for classes to implement. In addition to methods, interfaces can also include variables. These variables, known as interface variables or constants. Note that an interface variable is public, static, and final by...
4 min read
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, Flipkart etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to...
8 min read
In this section, we will learn what is a bouncy number and also create Java programs to check if the given number is bouncy. The bouncy number program frequently asked in Java coding tests and academics. Before understanding the bouncy number, first, we will understand what...
4 min read
Java pattern program enhances the coding skill, logic, and looping concepts. It is mostly asked in Java interview to check the logic and thinking of the programmer. We can print a Java pattern program in different designs. To learn the pattern program, we must have a...
13 min read
? Lambda expressions were introduced in Java 8 and are a powerful tool for writing concise, functional-style code. A lambda expression is an anonymous function that can be used to implement a method defined by a functional interface. Functional interfaces are interfaces that define only a single...
4 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