Write a method that when passed an array of integers arr, and an integer target, returns the last index at which target occurs in the array. Return -1 if the array is null or if target doesn’t exist in the array.
So I am using linear search to do this.
public static void linearSeach(int [] arr, target){
if( arr == null){
return -1;
}
count = 0;
for (int i = 0; i< arr.length; i++){
if( target == arr[i]){
count ++;
}
}
// I cannot return both count and -1 so here is what I thought I should do
if( count >= 0){
return count;
}
else {
return -1;
}}
Is this correct or the right approach?