Write a program in Java to allow the user to enter a positive integer.

If the user enters a negative integer, then convert it into a positive integer.

Now check if the entered integer is a unique number or not.

A **unique number** is a positive integer without any duplicate digits.

Examples are 283, 7045, etc.

```
import java.io.*;
class Unique{
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Number: ");
int n = Math.abs(Integer.parseInt(br.readLine()));
int a[] = new int[10];
for(int i = n; i > 0; i /= 10){
int d = i % 10;
a[d]++;
}
boolean status = true;
for(int i = 0; i < a.length; i++){
if(a[i] > 1){
status = false;
break;
}
}
if(status)
System.out.println(n + " is a unique number.");
else
System.out.println(n + " is not a unique number.");
}
}
```

In the above program, firstly we accept a positive integer from the user.

We use the `Math.abs()`

method to guarantee that the integer entered is positive.

Next, we create an integer type, one-dimensional array of size 10.

This array is used to keep track of the frequency of the individual digits present in the number.

Using the loop, we calculate the frequency of individual digits in the number.

Once done, we run another loop to check if there is any digit which has a frequency above 1.

If so, the status changes to false. Note that the status is true by default.

Finally, based on the status, we conclude whether a given number is unique or not.

It is also possible to check for the frequency of the digits in the first loop.

As soon as the frequency gets above 1, we can change the status to false and break out of the loop using the break statement.

This second approach will make the program more compact and efficient.

Below is code that implements the second approach:

```
import java.io.*;
class Unique{
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Number: ");
int n = Math.abs(Integer.parseInt(br.readLine()));
int a[] = new int[10];
boolean status = true;
for(int i = n; i > 0; i /= 10){
int d = i % 10;
a[d]++;
if(a[d] > 1){
status = false;
break;
}
}
if(status)
System.out.println(n + " is a unique number.");
else
System.out.println(n + " is not a unique number.");
}
}
```

## 6 replies on “Unique Number in Java”

Sir,what is the initial value of a[d] in the second approach of unique number ?

The array has 0 by default in all its blocks.

Sir,can we do the above program in this way?

import java.io.*;

class Unique

{

public static void main(String args[])throws IOException{

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int no,i,rm,flg=0;

System.out.print(“Enter a number:-“);

no=Integer.parseInt(br.readLine());

for(i=no;i>0;i=i/10)

{

rm=i%10;

int c=0;

for(int j=no;j>0;j=j/10)

{

int rm2=j%10;

if(rm2==rm)

c++;

}

if(c>1)

{

flg=1;

break;

}

}

if(flg==0)

System.out.println(“Unique Digit Number”);

else

System.out.println(“Not a Unique Digit Number”);

}

}

You can run and check it for yourself.

Sir,a program is given which states that:

Write a menu-driven program in Java to perform operations of Dequeue using linked list.

Here is the program that implements dequeue using a linked list.