Question
Print unique number in an array.
(A Unique number is a positive integer (without leading zeros) with no duplicate digits. For example 7, 135, 214 , 5243 are all unique numbers whereas 33, 3121, 200 are not.)
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class UniqueNumberInArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;
int n=0,i=0,j=0,flag=0,count=0,r=0,temp=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number of elements in the array");
n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
/* printing unique elements in an array */
System.out.println("unique Elements in an array");
for(i=0;i< n;i++)
{
for(j=0;j<=9;j++)
{
temp=arr[i];
count=0;
flag=0;
while(temp>0)
{
r=temp%10;
if(r==j)
/* if any digits are repeated, then it is not a UniqueNumber*/
{
count=count+1;
}
temp=temp/10;
}
if(count>1)
{
flag=1;
break;
}
}
if(flag ==0)
{
System.out.println(arr[i]+" ");
}
}
}
}