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

140
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 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