I want to find out if a key value is in array .. but it doesn't work..
class Solution {
public int solution(int key, int[] array1) {
int answer = 0;
for(int i=0; i < array1.length; i++) {
if(array1[i] == key) {
answer = i;
}
else return -1;
}
return answer;
}
}
ex ) key : 5 array1 : [1, 2, 3, 4, 5] answer : 4
-1if the key isn't at the first position of the array without checking the other positions.