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

145
Views
Encontrar y eliminar los 10 números más grandes y los 10 números más pequeños de la matriz

Me gustaría preguntar sobre un problema relacionado con una matriz. He generado una matriz con 100 enteros.

 //Generate 100 numbers var arr = []; while(arr.length < 100){ var r = Math.floor(Math.random() * 100) + 1; if(arr.indexOf(r) === -1) arr.push(r); } console.log(arr);

Mi problema es encontrar y eliminar los 10 máximos y mínimos principales de la matriz

 //Find the Minimum for (let i=0; i != 10; i++){ arr.splice(Math.min(...arr), 1) } //Find the Maximum for (let i=0; i != 10; i++){ arr.splice(Math.max(...arr), 1) }

y el resultado es que solo se elimina el 90 de la matriz. ¿Está luchando el código máximo y mínimo?

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

0

Debe obtener el índice del valor max y min del arr y luego usar splice . También cambia i != 10; a i<10

 var arr = []; while (arr.length < 100) { var r = Math.floor(Math.random() * 100) + 1; if (arr.indexOf(r) === -1) arr.push(r); } for (let i = 0; i < 10; i++) { const min = arr.indexOf(Math.min(...arr)); arr.splice(min, 1) } for (let i = 0; i < 10; i++) { const max = arr.indexOf(Math.max(...arr)); arr.splice(max, 1) }; console.log(arr.length)

about 4 years ago · Juan Pablo Isaza Report

0

Puede ordenar la matriz y luego usar el método de empalme para eliminar los primeros y últimos 10 elementos.

 var arr = []; while (arr.length < 100) { var r = Math.floor(Math.random() * 100) + 1; if (arr.indexOf(r) === -1) arr.push(r); } arr.sort((a, b) => +a - +b); arr.splice(0, 10); arr.splice(80, 10); console.log(arr.length);
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!