I want to find the last occurrence of x(13) in the array which is 6, but my code only finds the first occurrences which is 4, is there a way I can solve this?
/* Application Program */
int[] arr = {9, 12, 2, 8, 13, -8, 13, 9};
int index = lastIndexOf(arr, 13);
if(index != -1)
System.out.println(index);
else
System.out.println("not found");
/* Method */
public static int lastIndexOf(int[] arr, int x){
for(int i = 0; i<arr.length; i++){
if(arr[i] == x){
return i;
}
}
return -1;
}