JavaScript: Removing Duplicate Values from Array

Amandeep Kochhar
1 min readOct 10, 2018

--

Here i have discussed not One but Four Ways using which we can Remove Duplicate Values from Array in JavaScript:

let a = [1,2,2,3,45,65,65,66,34,45,1,2,3];

Using spread operator with Set:

let unique = [… new Set(a)];
console.log(unique);

Using Array.from with Set:

let unique = new Set(a);
unique = Array.from(unique);
console.log(unique);

Using array filter method:

const unique = a.filter((el, index) => a.indexOf(el) === index);
console.log(unique);

Using reduce method:

const unique = a.reduce((acc, curr)=> {
if(acc.indexOf(curr) === -1){
acc.push(curr);
}
return acc;
}, []);
console.log(unique);

If you find any of the method useful do clap!

--

--

No responses yet