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

182
Views
¿Cómo devolver todos los elementos de la matriz que no tienen su opuesto?

Tengo, por ejemplo, esta matriz [-3, 1, 2, 3, -1, 4, -2] y me gustaría devolver 4 porque no tiene su propio opuesto. He estado luchando durante varias horas para entender cómo implementar el algoritmo. Esto es lo que he hecho hasta ahora:

 let numbers = [-3, 1, 2, 3, -1, 4, -2]; let sortedNumebrs = []; //It returns the numbers array sorted function sortedArray() { sortedNumebrs = numbers.sort((a, b) => a - b); return sortedNumebrs; } // It return an array with all positive numbers function positive() { let positive = sortedArray().filter((e) => Math.sign(e) === 1); return positive; } // It return an array with all negative numbers function negative() { let negative = sortedNumebrs.filter((e) => Math.sign(e) === -1); negative = negative.sort((a, b) => a + b); return negative; } // It returns the array longer function minLength() { let minNum = Math.min(positive().length, negative().length); if (minNum === positive().length) { return positive() } else { eturn negative(); } } // It returns the array shorter function maxLength() { let maxNum = Math.max(positive().length, negative().length); if (maxNum === positive().length) { return positive() } else { return negative(); } } // Function that should return string if each numbers has its // own opposite otherwise 4 function opposite() { let result = (minLength() === maxLength()) ? true : false; if (result) { return 'Each element has own opposite'; } else { // some code } }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puedes intentar algo como esto:

 const yourArray =[-3, 1, 2, 3, -1, 4, -2]; const result = []; for (let el1 of yourArray) { let hasOpposite = false; for (let el2 of yourArray) { if (el1 === -el2) { hasOpposite = true; break; } } if (!hasOpposite) { result.push(el1); } } console.log(result); // [4]

o usando funciones de matriz:

 const yourArray = [-3, 1, 2, 3, -1, 4, -2]; const itemsWithoutOpposite = yourArray.filter(el1 => !yourArray.includes(-el1));
about 4 years ago · Juan Pablo Isaza Report

0

Aquí hay otro enfoque. Este supone que los duplicados también necesitan coincidir, por lo que si tengo [1, -1, 1], ese 1 final debería tener un -1 adicional para coincidir, en lugar de usar el -1 que coincidió con el 1 inicial. También lo hará no empareja un elemento consigo mismo, por lo que si tiene un 0 necesitará otro cero para emparejar.

 const yourArr = [-3, 1, 2, 3, -1, 4, -2]; const result = [...yourArr]; for (let i = 0; i < result.length; i++) { let el = result[i]; let inverse = result.indexOf(-el, i + 1); if (inverse !== -1) { result.splice(inverse, 1); result.splice(i, 1); i--; } } console.log(result);

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!