SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Unique Number Program in Java and Python

13 July 2013

Unique Number program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.

Question:

Write a program to input a number and check whether it is a Unique Number or not.

Note: A Unique Number is a positive integer without any repeated digit. For example, 7, 135 and 214 are unique numbers, whereas 33, 3121 and 300 are not unique numbers.

Enter a number: 3121 3121 is not a Unique Number Enter a number: 5243 5243 is a Unique Number

Algorithm:

Step 1: Start.

Step 2: Accept a number from the user.

Step 3: Convert the number into a string so that each digit can be accessed by its position.

Step 4: Store the length of the string in a variable.

Step 5: Set flag to 0, assuming that the number is unique.

Step 6: Select one digit at a time from the string.

Step 7: Compare the selected digit with every digit after it.

Step 8: If any two digits are equal, set flag to 1 and stop further checking.

Step 9: If flag is 0, display that the number is a Unique Number; otherwise, display that it is not a Unique Number.

Step 10: Stop.

Explanation:

The program converts the number into a string because it makes digit comparison simple. Once the number is stored as a string, each digit can be accessed using charAt() in Java or indexing in Python.

Two nested loops are used for comparison. The outer loop selects one digit from the string. The inner loop starts from the next position and compares the selected digit with all the remaining digits. This avoids comparing a digit with itself and also avoids repeating the same pair comparison.

The variable flag is used to remember whether a repeated digit has been found. It is initialized to 0, meaning no repetition has been found yet. If any matching pair of digits is found, flag becomes 1.

After the loops finish, the value of flag decides the result. If flag is still 0, no digit was repeated and the number is unique. If flag is 1, at least one digit was repeated and the number is not unique.

A unique number has no repeated digits. The program must compare digits to ensure that each digit appears only once. This may be done using a frequency array of size 10 or by comparing every digit with the remaining digits. If a frequency array is used, each extracted digit increases its corresponding count; any count greater than 1 proves repetition. If nested digit extraction is used, copies of the number are needed so that comparisons do not destroy the original value too early.

Java Program:

Java
/**
* The class UniqueNumber accepts a number and checks whether
* any digit is repeated in it.
*
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;

class UniqueNumber
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int n = sc.nextInt();

        /*
        * The number is converted to a string so that each digit
        * can be compared using its position.
        */
        String s = Integer.toString(n);
        int len = s.length();

        /*
        * flag remains 0 if no repeated digit is found.
        * It becomes 1 as soon as a repeated digit is detected.
        */
        int flag = 0;

        /*
        * The outer loop selects one digit.
        * The inner loop compares it with all digits after it.
        */
        for(int i = 0; i < len - 1; i++)
        {
            for(int j = i + 1; j < len; j++)
            {
                if(s.charAt(i) == s.charAt(j))
                {
                    flag = 1;
                    break;
                }
            }
        }

        if(flag == 0)
        System.out.println(n + " is a Unique Number");
        else
        System.out.println(n + " is not a Unique Number");
    }
}

Equivalent Python Program:

Python
n = int(input("Enter a number: "))

# The number is converted to a string so that each digit can be accessed.
s = str(n)
length = len(s)

# flag remains 0 if no repeated digit is found.
flag = 0

# The outer loop selects one digit.
for i in range(0, length - 1):

    # The inner loop compares the selected digit with later digits.
    for j in range(i + 1, length):
        if s[i] == s[j]:
            flag = 1
            break

# The final result depends on whether a repeated digit was found.
if flag == 0:
    print(n, "is a Unique Number")
else:
    print(n, "is not a Unique Number")

Output:

Enter a number: 3121 3121 is not a Unique Number Enter a number: 5243 5243 is a Unique Number

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →