Recursion Last Index

Dear Sir,

I have written the code like this for finding the last index of number in an array. My code is little different form the code in course.

Can you check and let me know if my code is correct ? I have tested for -1 and non -1.

Do you see any issue with this code ?

public static int lastIndex(int[] arr, int si, int data){

	int match = -1;
			
	if(arr.length == si )
		return -1;
	
	if(arr[si] == data ) {
		match = si;
	}

	int result = lastIndex(arr,si+1,data);
	
	if( match > result ) 
		return match;
	else
		return result;
	
}

Hey @ your code looks fine to me. You could have also used code like this given in your course. but still your code is fine.

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.