Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

295
Views
¿Cómo encontrar el mínimo en una matriz excluyendo 0?

Tengo una matriz con algunos valores y quiero encontrar el valor mínimo de esa matriz para imprimir el índice que tiene este valor. En esta matriz, uno de los valores es 0. Supongo que para encontrar el índice, debemos iterar a través de esta matriz para encontrar el mínimo, pero no el 0, y devolver el índice. ¿Pueden ayudarme a entender qué estoy haciendo mal con las iteraciones? No puedo encontrar el número mínimo.

Esta es la matriz:

 [ 10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75, 3.75 ]

Actualmente estoy atrapado aquí:

 var smallest = 0 var biggest = 0 for (let i = 0; i < merged.length; i++) { if (merged[i] > biggest && merged[i] != 0) { biggest = merged[i]; } else merged[i] < smallest ? smallest = merged[i] : smallest = merged[i]; console.log('the biggest is', biggest, 'in the iteration', i) console.log('the smallest is', smallest, 'in the iteration', i) } console.log('min-> : ', smallest, biggest);

Eso da esto:

 the biggest is 10 in the iteration 0 the smallest is 0 in the iteration 0 the biggest is 10 in the iteration 1 **the smallest is 5 in the iteration 1** the biggest is 10 in the iteration 2 **the smallest is 6 in the iteration 2** the biggest is 10 in the iteration 3 the smallest is 5.5 in the iteration 3 the biggest is 10 in the iteration 4 the smallest is 3.75 in the iteration 4 the biggest is 10 in the iteration 5 the smallest is 0 in the iteration 5 the biggest is 10 in the iteration 6 the smallest is 4.25 in the iteration 6 the biggest is 10 in the iteration 7 the smallest is 3 in the iteration 7 the biggest is 10 in the iteration 8 the smallest is 5.5 in the iteration 8 the biggest is 10 in the iteration 9 the smallest is 6.75 in the iteration 9 the biggest is 10 in the iteration 10 the smallest is 8 in the iteration 10 the biggest is 10 in the iteration 11 the smallest is 9.25 in the iteration 11 the biggest is 10 in the iteration 12 the smallest is 4 in the iteration 12 the biggest is 15 in the iteration 13 the smallest is 4 in the iteration 13 the biggest is 15 in the iteration 14 the smallest is 4.25 in the iteration 14 the biggest is 15 in the iteration 15 the smallest is 6 in the iteration 15 the biggest is 15 in the iteration 16 the smallest is 6 in the iteration 16 the biggest is 15 in the iteration 17 the smallest is 4.75 in the iteration 17 the biggest is 15 in the iteration 18 the smallest is 3.75 in the iteration 18 min-> : 3.75 15

Como puede ver arriba, el más pequeño no debe cambiar de 5 a 6 . El mínimo debe ser 3 .

Muchísimas gracias.

Duda resuelta, gracias a todos por su tiempo y ayuda!!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Primero puede encontrar el mínimo sin cero y luego encontrar el índice usando lo siguiente, esto se puede cambiar para encontrar también el índice máximo.

 const arr = [ 10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75, 3.75 ] const minIndex = arr.indexOf(Math.min.apply(null, arr.filter(Boolean))) console.log(minIndex)

Aquí hay un enfoque iterativo en caso de que sea más claro:

 const findMin = (arr) => { let min; for(let i =0; i < arr.length; i++){ if(!min && arr[i]!==0) min = arr[i] if(arr[i] < min && arr[i]!==0) min = arr[i] } return arr.indexOf(min) } const arr = [ 10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75, 3.75 ] console.log(findMin(arr))

about 4 years ago · Juan Pablo Isaza Report

0

Con respecto a su función fallida, informa la iteración actual, pero parece que no realiza un seguimiento de cada iteración o, mejor aún, debería llevar el número mínimo en cada iteración.

  • Difundir la matriz con el operador ...

  • matriz .filter() directamente y devolverá valores verdaderos que excluyen 0.

  • Luego usa Math.min()

  • Finalmente, obtenga el índice con .indexOf()

 let data = [ 10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75, 3.75 ]; let out = data.indexOf(Math.min(...data.filter(n => n))) console.log(out);

about 4 years ago · Juan Pablo Isaza Report

0

Puede escanear la matriz solo una vez para encontrar el mínimo y el índice.

 var merged = [ 10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75, 3.75, ]; let [minVal, minIndex] = merged.reduce( function ([minVal, minIndex], currentVal, currentIndex) { if (currentVal!= 0 && minVal > currentVal) { minVal = currentVal; minIndex = currentIndex; } return [minVal, minIndex]; }, [Infinity, -1] ); console.log(minVal, minIndex);

Además, funciona.

 var merged = [ 10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75, 3.75, ]; var smallest = Infinity; for (let i = 0; i < merged.length; i++) { if (merged[i] < smallest && merged[i] != 0) { smallest = merged[i]; } console.log("the smallest is", smallest, "in the iteration", i); } console.log("min-> : ", smallest);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!