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

102
Views
Filter out Mirrors in array
let mirror_arr=[ [1,2],[3,2],[1,4],[8,1],[5,4],[2,1],[3,5],[1,8],[3,2],[2,3] ]

filtered_mirror=[ [1,2],[3,2],[1,4],[8,1],[5,4],[3,5] ];

This algorithm filter out all the mirror ones. For example, [1,2] is a mirror of [2,1], so it will be filtered by removing the mirrors. Are there any short method to do this in Javascript? Thank you for reading :) My codepen solution is here and I am almost solved it. It is just that I have to remove the duplicate ones.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I've assumed that you don't want duplicates either as you didn't have a second [3,2] in your expected output. From your codepen it looks like you're familiar with Set and JSON.stringify .

let mirror_arr = [ [1,2],[3,2],[1,4],[8,1],[5,4],[2,1],[3,5],[1,8],[3,2],[2,3] ];

let filtered = [];
let found_set = new Set();

for(let i = 0; i < mirror_arr.length; i++) {
  let item = mirror_arr[i];
  let string_item = JSON.stringify(item);
  
  if(!found_set.has(string_item)) {
    filtered.push(item);
    found_set.add(string_item);
    found_set.add( JSON.stringify([...item].reverse()) );
  }
}

console.log(JSON.stringify(filtered));
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Santiago Trujillo 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!