can anyone help me find what wrong with this code. I am trying to write a function method that determine the second to the last occurrence of a target in an integer array. Return -1 if not in the array.
public static int findSecondToLast(int [] a, int target) {
int [] b = new int[countOfTarget (a,target)];
int k =0;
for (int i = 0; i < a.length; i++){
if (a[i]==target){
b[k]=i;
k++;
return b[ countOfTarget (a,target) - 1];
}
}
return -1;
}
public static int countOfTarget (int[]a, int t){
int count = 0;
for (int i=0; i < a.length; i++) {
if (a[i] == t)
count++;
}
return count;
}