Is this approach better

What I did in this code was that I created a static variable = index which stored the starting index ( si ) for each function call under the condition that when arr[si] == data.
I AM GONNA SHARE THE CODE

package recursion; public class LastIndex { public static int index = 0; public static void main(String[] args) { int[] arr = { 3, 8, 1, 8, 8, 7 , 8 }; int si = 0, data = 80; System.out.println(lastIndex(arr, si, data)); } public static int lastIndex(int[] arr, int si, int data) { if (si == arr.length) { return -1; // Base Case } if (arr[si] == data) { index = si; lastIndex(arr, si + 1, data); return index; } return lastIndex(arr, si + 1, data); } }

yaa this approach is also correct…but in this case you can make your function return type as void…dont both retusrn and static approaches

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.