Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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 = [x[i], y[i]]
  coordinates.push(c);
}
const coordArray = coordinates.reduce((p, next) => {
  if (!p.includes(JSON.stringify(next)) && !p.includes(JSON.stringify([...next].reverse()))) {
    p.push(JSON.stringify(next));
  }
  return p;
},[]).map(JSON.parse)
console.log(coordArray)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading