Reputation: 1
I'm looking to make a code that will display unique numbers in array x not in array y. This is what I have so far. What am I doing wrong?
int unique=0;
int i,j,k;
int x[]={1,2,3,4,5};
int y[]={1,3,5,7,9};
for(i=0;i<x.length;i++)
{
for(j=0;j<y.length;j++)
{
if(x[i] == y[j])
{
unique = 1;
}
}
if(unique == 0)
{
System.out.print(x[i]);
unique =0;
}
}
The code is producing 000011111222233334444.
Upvotes: 0
Views: 78
Reputation: 1234
I think you should reset the unique value. So just add unique = 0;
at the endo of the first loop:
int unique = 0;
int i, j, k;
int x[] = {1, 2, 3, 4, 5};
int y[] = {1, 3, 5, 7, 9};
for (i = 0; i < x.length; i++) {
for (j = 0; j < y.length; j++) {
if (x[i] == y[j]) {
unique = 1;
}
}
if (unique == 0) {
System.out.print(x[i]);
unique = 0;
}
unique = 0;
}
Upvotes: 2