I am attempting to write a function that finds the last occurrence of a target in a vector by modifying a linear search function.
private int linearSearchRecursive(int[] input, int key,int index) {
if (index == 0) {
return -1;
}
if (input[index] == key) {
return index;
}
else
return linearSearchRecursive(input,key,--index);
}
I thought of a way to make it work by using a helper function...
public static int findLastOccurance(int[] items, int key){
return linearSearchRecursive(items, key, items.length - 1);
}
Or something of that nature, but was wondering if there was an easier way where I could use only one function but keep the recursiveness?