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

181
Views
Eliminar duplicados y espejo de coordenadas [x, y]

sea x=[1, 2 ,6,3,5, 5 , 5 , 4 ,4];

sea y=[3, 4 ,3,5,2, 4 , 4 , 2 ,6];

 expected_x=[1,2,6,3,5,5,4] expected_y=[3,4,3,5,2,4,6]

Piense en x e y como coordenadas. [1,3] será el primer punto y [4,6] será el último punto.

Si un [X,Y] tiene duplicados, solo uno de los [X,Y] se mostrará en la salida esperada (sin duplicado). Y si hay un espejo como [X,Y] que es un espejo de [Y,X] con ambos en el mismo índice.

Este es el código que he escrito para una sola matriz para que la matriz sea única. Sin embargo, no estoy seguro de cómo usarlo con 2 matrices separadas que representan las coordenadas x e y. Cualquier ayuda será apreciada :)

 let chars = ['A', 'B', 'A', 'C', 'B']; let uniqueChars = [...new Set(chars)]; console.log(uniqueChars);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Utilizar este:

 let x=[1,2,6,3,5,5,5,4,4]; let y=[3,4,3,5,2,4,4,2,6]; const coordinates = []; let i = -1; while ( x[++i] ) { const c = { index: i, value: [x[i], y[i]] } coordinates.push(c); } const coordArray = coordinates.reduce((p, next) => { if (!p.values.includes(JSON.stringify(next.value)) && !p.values.includes(JSON.stringify([...next.value].reverse()))) { p.values.push(JSON.stringify(next.value)); p.indexes.push(next.index); } return p; },{ indexes: [], values: [] }) coordArray.values = coordArray.values.map(JSON.parse) console.log(coordArray)

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar un for loop e iterar ambas matrices juntas, ya que tienen la misma longitud (siendo un par x,y) entre sí.

También puede mantener un "historial" de duplicados y espejos. Luego, todo lo que necesita hacer mientras itera es verificar el historial. Si no hay ninguna coincidencia, agregue el actual a las matrices de resultados y luego actualice el historial.

 let x=[1,2,6,3,5,5,5,4,4]; let y=[3,4,3,5,2,4,4,2,6]; let h=[]; // history let rx = []; // result x let ry = []; // result y for (let i = 0; i < x.length && i < y.length; i++) { // The if line (with include()) would be nice if it worked, but it didn't because of // always returning false. // Instead I will have to manually search. // if (h.includes([x[i], y[i]]) || h.includes([y[i], x[i]])) { let found = false; for (let s = 0; s < h.length; s++) { // check for duplicate if (h[s][0] == x[i] && h[s][1] == y[i]) { found = true; break; } // check for mirror if (h[s][0] == y[i] && h[s][1] == x[i]) { found = true; break; } } if (found) { // do nothing, its a duplicate or mirror console.log("duplicate or mirror detected on index " + i); } else { // update results rx.push(x[i]); ry.push(y[i]); // update history h.push([ x[i], y[i] ]); } } console.log("rx: " + rx); console.log("ry: " + ry);

En resumen, .include() hubiera sido bueno, pero aparentemente la matriz por referencia rompió mi lógica prevista. No sé. Pero lo anterior separó esas preocupaciones mediante una búsqueda literal de "historia", que alteraría el booleano "encontrado" para saber si existía un duplicado o un espejo.

Obviamente, este código podría acortarse a menos de 10 o 7 líneas, pero quería trabajar en él porque era interesante y el enfoque utilizado demuestra cómo se pueden usar los bucles regulares for resolver tales problemas de "iteración".

Espera que ayude.

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!