Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
Remove duplicates and mirror from [x,y] coordinates

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

let 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]

Think of x and y as coordinates.[1,3] will be first point and [4,6] will be last point.

If a [X,Y] has duplicates, only one of the [X,Y] will be displayed in the expected output (no duplicate). And if, there is a mirror like [X,Y] which is a mirror of [Y,X] with both at the same index.

This is the code I have written for just one array to make the array unique. However, I am unsure on how to use it with 2 seperate arrays representing x and y coordinates. Any help will be appreciated :)

let chars = ['A', 'B', 'A', 'C', 'B'];
let uniqueChars = [...new Set(chars)];

console.log(uniqueChars);
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Use this:

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 Relatório

0

You can use a for loop and iterate both arrays together, since they have the same length (being an x,y pair) to each other.

You can also keep a "history" of duplicates and mirrors. Then all you need to do while iterating is check the history. If there is no match, append the current to the result arrays, then update the history.

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);

In short, .include() would have been nice, but apparantly the array by reference broke my intended logic. I don't know. But the above separated those concerns out by a literal search of "history", which would alter the "found" boolean to know whether a duplicate or mirror existed.

Obviously this code could like be shortened into less than 10 or 7 lines, but I wanted to work on it because it was interesting and the approach used demonstrates how regular for loops could be used to solve such "iteration" problems.

Hopes it helps.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda