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

95
Views
Filtering an array for only unique values

for example if arr = [1,1,2,3,5,5] this will return [1,2,3,5].

function returnUnique(arr) {
      
      let unique = [...new Set(arr)];
      return unique;
    };

my goal is to return only 2,3

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

function returnUnique(arr) {
  return arr.filter(e => arr.indexOf(e) === arr.lastIndexOf(e));
}

console.log( returnUnique([1,1,2,3,5,5]) );

about 4 years ago · Juan Pablo Isaza Report

0

You can easily achieve the result using Map

const arr = [1, 1, 2, 3, 5, 5];

const map = new Map();
arr.forEach((n) => map.set(n, (map.get(n) ?? 0) + 1));

const result = [];
for (let [k, v] of map) {
  if (v === 1) result.push(k);
}

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Use reduce function, for example:

const array = [1, 1, 1, 2, 3, 3, 4, 5, 6, 6, 6];

const uniqueValues = (array) => array.reduce((arr, item) => {
if (!arr.some((filteredItem) =>
        JSON.stringify(filteredItem) === JSON.stringify(item)
    ))
    arr.push(item);
return arr;
}, []);
console.log(uniqueValues(array))

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!