How to implement lastIndexOf function on int array in Java

1 Answer

0 votes
public class Program {
    public static void main(String[] args) {
        int[] arr = {6, 7, 8, 4, 2, 1, 8, 1, 7, 5, 8, 9, 5, 3};

        System.out.println(lastIndexOf(arr, 8));
    }

   public static int lastIndexOf(int[] arr, int value) {
        for (int i = arr.length - 1; i >= 0; i--) {
            if (arr[i] == value) {
                return i;
            }
        }
        return -1;
    }
}


 
/*
run
 
10
 
*/


 



answered Jun 1, 2024 by avibootz

Related questions

...