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

142
Visualizações
for loop method for finding non-sequential slots (holes) in an array

Just as the title says - I'm trying to find the first empty (missing) number (hole) in an array using for-loop or, if the array is sequential and starting from 1 through 7 for example, I'll need the next integer after the last array item, in this case 8.

Need only one hole per whole iteration returning the found value.

My logic is ok when it comes to the first condition (array starting with a value higher than 1) but the from the next to conditions the last one is always picked up.

In a nutshell:

  • in an array of [2,3,4,5,6,7] I need "1" and I get it;
  • in an array of [1,2,3,4,6,7] I need "5" but I get "8".
  • in an array of [1,2,3,4,5,6,7] I need "8" and I get it.

const numbers = [1, 2, 3, 4, 6, 7];

document.getElementById("demo").innerHTML = numbers;
let hole;

holeFinder()

function holeFinder() {
  for (let i = 0; i < numbers.length; i++) {
    if (numbers[0] > 1) {
      /*  console.log(numbers[0]) */
      hole = 1;
      document.getElementById("demo").innerHTML += "<br>1: hole: " + hole;
      return hole;
    }

    if (numbers[i] > 1) {
      if ((numbers[i] - numbers[i - 1]) > 1) {
        hole = (numbers[i - 1] + 1)
        document.getElementById("demo").innerHTML += "<br>2: hole: " + hole;
        return hole;
      }

      if ((numbers[i] - numbers[i - 1]) == 1) {
        hole = numbers[numbers.length - 1] + 1;
        document.getElementById("demo").innerHTML += "<br>3: hole: " + hole;
        return hole;
      }
    }
  }
}
<h2>for loop method for finding non-sequential slots (holes) in an array</h2>
<p id="demo"></p>

JSFIDDLE

What's wrong with my logic?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can do something like this

const arr1 = [1, 2, 3, 4, 6, 7]
const arr2 = [1, 2, 4, 5, 6, 7]
const arr3 = [1, 2, 3, 4, 5, 6, 7]


const findHole = arr => {
 for(let i = 0; i < arr.length; i++){
  if(arr[i] !== i + 1){
    return i + 1
  }
 }
 return arr.length + 1
}

console.log(findHole(arr1))
console.log(findHole(arr2))
console.log(findHole(arr3))

about 4 years ago · Juan Pablo Isaza Relatório

0

You could also use a while loop and find the missing number to break the loop.

const one = [2, 3, 4, 5, 6, 7];
const two = [1, 2, 3, 4, 6, 7];
const three = [1, 2, 3, 4, 5, 6, 7];

function findHole(numbers) {
  let count = 0;
  let hole = null;

  while (!hole) {
    count++
    const number = count;
    
    // if the array does not contain number, assign the number
    // as the missing hole number
    if (!numbers.includes(number)) {
      hole = number;
    }
  }

  return hole;
}

document.getElementById("demo").innerHTML += "1: hole: " + findHole(one) + '<br>';
document.getElementById("demo").innerHTML += "2: hole: " + findHole(two) + '<br>';
document.getElementById("demo").innerHTML += "3: hole: " + findHole(three) + '<br>';
<div id="demo"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

You enter the second part of the second if statement

if ((numbers[i] - numbers[i - 1]) == 1) {
    hole = numbers[numbers.length - 1] + 1;
    document.getElementById("demo").innerHTML += "<br>3: hole: " + hole;
    return hole;
}

on the second iteration of the loop, so as long as you don't find the hole at the first index, it will always return the last array element increased by one. You should check this condition only at the last element of your array.

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