Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda