Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
Finding the last occurrence of x in the array

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;
}
over 4 years ago · Santiago Trujillo
4 Respuestas
Responde la pregunta

0

Iterate the array backwards. Like,

public static int lastIndexOf(int[] arr, int x) {
    for (int i = arr.length - 1; i >= 0; i--) {
        if (arr[i] == x) {
            return i;
        }
    }
    return -1;
}
over 4 years ago · Santiago Trujillo Denunciar

0

Method-1

If you are scanning from the first element to the last element, you should store the result in a variable.

public static int lastIndexOf(int[] arr, int x)
{
    int resultIndex = -1;
         
    for(int i = 0; i < arr.length; i++)
        if(arr[i] == x)
            resultIndex = i;

    return resultIndex;
}
Method-2

If you're scanning from the last element to the first element, you can return the result directly from the method.

public static int lastIndexOf(int[] arr, int x)
{
    for(int i = arr.length - 1 ; i >= 0; --i)
        if(arr[i] == x)
            return i;
    
    return -1;
}
over 4 years ago · Santiago Trujillo Denunciar

0

you want to find the last occurrence of 13 but you are returning the index in very first match itself.

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

In order to return the last occurrence you can modify the code and return the most recent index.

public static int lastIndexOf(int[] arr, int x){
int lastIndex = -1
    for(int i = 0; i<arr.length; i++){
        if(arr[i] == x){
            lastIndex = i;
        }
    }
    return lastIndex;
}
over 4 years ago · Santiago Trujillo Denunciar

0

Using java Map based approach.

 public static int lastIndexOfV1(int[] arr, int x) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            map.put(arr[i], i);
        }
        return map.get(x);
    }
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda