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

104
Vistas
Return missing number in array of integers 1 - 9

I ran the code using different arrays missing a number between 1 - 9, but it kept returning -1.

function findMissing(arrOfNumbers) {
  const integers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  for (let index = 0; index < arrOfNumbers.length; index++) {
    if (integers[index] !== arrOfNumbers[index]) {
      return integers[index];
    }
    return -1;
  }
}

console.log(findMissing([0, 1, 3, 4, 6, 7, 8, 9])); //returns -1 instead of 5

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If I understood the problem, you want missing elements from array 2. if so, simply use,

arr1.filter(x=> !arr2.includes(x));

In your case

const array1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const array2 = [0, 1, 3, 4, 6, 7, 8, 9];
const result = array1.filter(x=> !array2.includes(x)); // returns 2 , 5

console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can spread the array over an object and compare the key and value pairs of the object. If key and value are different then that means some value is missing in the input array that is out of range. Note the use of !=, it is intentional to compare values and not types.

function findMissing(arrOfNumbers) {
  const obj = {...arrOfNumbers};
  for(const [key, val] of Object.entries(obj)) {
    if(key != val) {
      return key;
    }
  }
  return -1;
}

// returns 2 since it is the first missing value in the order
console.log(findMissing([0, 1, 3, 4, 6, 7, 8, 9])); 

about 4 years ago · Juan Pablo Isaza 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