# How to Remove Duplicate Values from an Array ## filter() The `filter()` method creates a new array of elements that pass the conditional we provide. In other words, if the element passes and returns `true`, it will be included in the filtered array. And any element that fails or return `false`, it will be NOT be in the filtered array. ``` javascript let arr = ['html', 'css', 'js', 'css', 'js']; function removeDuplicates(data) { return data.filter((value, index) => arr.indexOf(value) === index); } console.log(removeDuplicates(arr)); ``` ## forEach() ``` javascript let arr = ['html', 'css', 'js', 'css', 'js']; function removeDuplicates(data) { let unique = []; data.forEach(element => { if (!unique.includes(element)) { unique.push(element); } }) return unique } console.log(removeDuplicates(arr)); ``` ## reduce() The `reduce` method is used to reduce the elements of the array and combine them into a final array based on some reducer function that you pass. ``` javascript let arr = ['html', 'css', 'js', 'css', 'js']; function removeDuplicates(data) { let unique = data.reduce((acc, cur) => { if (acc.indexOf(cur) < 0) { acc.push(cur); } return acc; }, []); return unique; } console.log(removeDuplicates(arr)); ``` ## Set() `Set` is a new data object introduced in ES6. Because `Set` only lets you store unique values. When you pass in an array, it will remove any duplicate values. ``` javascript let arr = ['html', 'css', 'js', 'css', 'js']; function removeDuplicates(data) { return [...new Set(data)]; } console.log(removeDuplicates(arr)); ``` ``` javascript let arr = ['html', 'css', 'js', 'css', 'js']; console.log(Array.from(new Set(arr))); ``` ## References - [How to Remove Array Duplicates in ES6](https://medium.com/dailyjs/how-to-remove-array-duplicates-in-es6-5daa8789641c) - [Performance of Javascript Array Ops](https://blog.usejournal.com/performance-of-javascript-array-ops-2690aed47a50) - [@webdevofficial](https://www.instagram.com/p/B_fLJCjjQuT/) ###### tags: `JavaScript`