I wrote the following code for solving a problem but it doesn’t work. The link to the question is here.
public boolean linearIn(int[] outer, int[] inner) {
boolean result = false;
if(inner.length == 0)
return true;
index: for(int x: inner) {
for(int y: outer) {
if(x==y) {
result = true;
break;
}
else {
result = false;
break index;
}
}
}
return result;
}
The problem is in the else part of. As you can’t conclude that the element in not in the inner array if just one comparison fails:
To fix this you might do: