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

132
Vistas
Encontrar la primera aparición de cada dígito en una matriz

Tomando cada número de cuatro dígitos de una matriz por turno, devuelva el número en el que se encuentra cuando se han descubierto todos los dígitos del 0 al 9. Si no se pueden encontrar todos los dígitos, devuelva "¡Dígitos faltantes!"

Intenté recorrer y luego establecer un condicional if (i != i+1) empujar a una nueva matriz, esto solo me dio la matriz, es evidente que mi lógica es incorrecta. Podría alguien ayudarme

Por ejemplo, llamando a esta función con

 arr = findAllDigits([5175, 4538, 2926, 5057, 6401, 4376, 2280, 6137, 8798, 9083])

el código debería devolver 5057.

mientras llama

 arr = findAllDigits([4883, 3876, 7769, 9846, 9546, 9634, 9696, 2832, 6822, 6868])

debe devolver "números que faltan"

 function findAllDigits(arr) { newArr = []; for (let i = 0; i < arr.length; i++) { if (arr[i] != arr[i + 1]) newArr.push(arr[i]); console.log(newArr); } }

¿Necesito dividir porque está tomando todo antes de la coma como un número y luego iterar?

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

0

Puedes usar Establecer aquí

Recorra la array y luego cree un set . Debe devolver el número actual si el tamaño del set se convierte en 10 porque necesita verificar 0-9

 function findAllDigits(arr) { const set = new Set(); for (let n of arr) { String(n) .split("") .forEach((c) => set.add(c)); if (set.size === 10) return n; } return "Missing digits!"; } const arr1 = [5175, 4538, 2926, 5057, 6401, 4376, 2280, 6137, 8798, 9083]; const arr2 = [4883, 3876, 7769, 9846, 9546, 9634, 9696, 2832, 6822, 6868]; console.log(findAllDigits(arr1)); console.log(findAllDigits(arr2));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Su ciclo for solo está verificando si la entrada de la matriz es igual a la siguiente. Debe dividir los dígitos dentro de cada entrada y almacenarlos individualmente:

 function findAllDigits(arr) { newArr = []; for (let i = 0; i < arr.length; i++) { // now iterate the individual digits const entryAsString = arr[i].toString(); for (let j = 0; j < entryAsString.length; j++) { // if we haven't seen the digit before, add it to the array if(!newArr.includes(j) { newArr.push(j); } } // we know we have all digits when newArr is 10 entries long if (newArr.length) { console.log(arr[i]); // you can also return this value here } } }
about 4 years ago · Juan Pablo Isaza Denunciar

0

Una solución más:

 const arr1 = [5175, 4538, 2926, 5057, 6401, 4376, 2280, 6137, 8798, 9083]; const arr2 = [4883, 3876, 7769, 9846, 9546, 9634, 9696, 2832, 6822, 6868]; const findAllDigits = (arr) => { // Declare new Set: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set const digits = new Set(); // return the first item from array that fits the condition, // find() method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find return arr.find((curr) => ( // String(5175) -> '5175' : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String // [...'5175'] -> ['5','1','7','5'] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax // .forEach(digits.add, digits) - forEach with callback function and context : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach // comma operator lets get rid of return : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator [...String(curr)].forEach(digits.add, digits), // condition - is find() method need to return an item (digits.size === 10) // if returned value is not undefined or null return finded number oterwise error string // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator )) ?? "Missing digits!"; }; console.log(findAllDigits(arr1)); //5057 console.log(findAllDigits(arr2)); //Missing digits!
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