Remove duplicates from an array in javascript ES6

1 month ago23k times

The easiest way to remove the duplicates from an array is using a set function in javascript.

const letters = ['a','a','b','c','c','c','d','e'];
const unique = [...new Set(letters)];
console.log(unique); // a,b,c,d,e


Other answers

duplicates from an array in JavaScript ES6 can also be removed using reduce function


const letters = ['a','a','b','c','c','c','d','e'];

const unique = letters.reduce((result,letter) => {

  return result.includes(letter) ? result : [...result,letter];
 
},[]);

console.log(unique);



duplicates can also be removed using filter method. try using code below


const letters = ['a','a','b','c','c','c','d','e'];
const unique = letters.filter((letter,index) => {
    return letters.indexOf(letter) === index;
},[]);
console.log(unique);

Add Answer

Add a codeAdd Code
Remove adsremove