Can someone please explain why System.out.println("Doesn't repeat: "+a[i]);
does not print anything?
import java.util.Arrays;
import java.util.Random;
public class Practice {
public static void main(String[] args) {
int []a=new int[100];
Random rand =new Random();
for(int i=0;i<100;i++)
{
a[i]=rand.nextInt((100-1)+1)+1;
}
Arrays.sort(a);
for(int i=0;i<100;i++)
{
System.out.print(a[i]+ " ");
}
boolean flag;
System.out.print(" ");
System.out.print(" ");
for(int i=0;i<100;i++)
{
flag=false;
for(int j=0;j<100;j++)
{
if(a[i]==a[j])
{
flag=true;
break;
}
}
if(flag==false)
{
System.out.println("Doesn't repeat: "+a[i]);
}
}
}
}
I am getting only the current array of 100 elements and the lines with elements which doesn't appear twice or more don't show up.
Edit:
import java.util.Arrays;
import java.util.Random;
public class Practice {
public static void main(String[] args) {
int []a=new int[100];
Random rand =new Random();
for(int i=0;i<100;i++)
{
a[i]=rand.nextInt((100-1)+1)+1;
}
Arrays.sort(a);
for(int i=0;i<100;i++)
{
System.out.print(a[i]+ " ");
}
boolean flag;
System.out.print(" ");
System.out.print(" ");
for(int i=0;i<100;i++)
{
flag=false;
for(int j=0;j<100;j++)
{
if (i==j) {
continue;
}
else if(a[i]==a[j])
{
flag=true;
break;
}
}
if(flag==false)
{
System.out.println("Doesn't repeat: "+a[i]);
}
}
}
}
For now this is the best I can get, but the code excludes equal elements even if they are the unique ones.