In this article, we will discuss the methods to remove duplicate elements from a Javascript array. There are various methods to remove duplicates in the array.
These are the following ways:
- Using filter() Method
- Using set() Method
- Using reduce() Method
- Using indexOf() Method
- Using forEach() Method
- Using Underscore.js _.uniq() Function
Below all the methods are described with proper examples:
Method 1: Using filter() Method
The filter() method creates a new array of elements that pass the condition we provide. It will include only those elements for which true is returned. We can remove duplicate values from the array by simply adjusting our condition.
Example: In this example, we will see the use of the filter() method.
Javascript
let arr = [ "apple" , "mango" , "apple" , "orange" , "mango" , "mango" ]; function removeDuplicates(arr) { return arr.filter((item, index) => arr.indexOf(item) === index); } console.log(removeDuplicates(arr)); |
Output:
["apple", "mango", "orange"]
Method 2: Using set() Method
This method sets a new object type with ES6 (ES2015) that allows you to create collections of unique values.
Example: In this example, we will see the use of the set() method.
Javascript
let arr = [ "apple" , "mango" , "apple" , "orange" , "mango" , "mango" ]; function removeDuplicates(arr) { return [... new Set(arr)]; } console.log(removeDuplicates(arr)); |
Output:
["apple", "mango", "orange"]
Method 3: Using forEach() Method
By using the forEach() method, we can iterate over the elements in the array, and we will push into the new array if it doesn’t exist in the array.
Example: In this example, we will see the use of the forEach() method.
Javascript
let arr = [ "apple" , "mango" , "apple" , "orange" , "mango" , "mango" ]; function removeDuplicates(arr) { let unique = []; arr.forEach(element => { if (!unique.includes(element)) { unique.push(element); } }); return unique; } console.log(removeDuplicates(arr)); |
Output:
["apple", "mango", "orange"]
Method 4: Using reduce() Method
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.
Example: In this example, we will see the use of the reduce() method.
Javascript
let arr = [ "apple" , "mango" , "apple" , "orange" , "mango" , "mango" ]; function removeDuplicates(arr) { let unique = arr.reduce( function (acc, curr) { if (!acc.includes(curr)) acc.push(curr); return acc; }, []); return unique; } console.log(removeDuplicates(arr)); |
Output:
["apple", "mango", "orange"]
Method 5: Using indexOf() Method
The indexOf() method is used to find the first index of occurrence of an array element. we can iterate over the elements in the array, and we will push into the new array if it doesn’t exist in the resultant array.
Example: In this example, we will see the use of the indexOf() method.
Javascript
let arr = [ "apple" , "mango" , "apple" , "orange" , "mango" , "mango" ]; function removeDuplicates(arr) { let unique = []; for (i = 0; i < arr.length; i++) { if (unique.indexOf(arr[i]) === -1) { unique.push(arr[i]); } } return unique; } console.log(removeDuplicates(arr)); |
Output:
['apple', 'mango', 'orange']
Method 6: Using a third-party library
We can also use a third-party library such as Lodash or Underscore.js to remove duplicate elements from a Javascript array. The _.uniq() function returns the array which does not contain duplicate elements.
Example: In this example, we will use the _.uniq() function.
Javascript
// Requiring the lodash library const _ = require( "lodash" ); // Original array let y = ([1, 2, 2, 3, 4, 3, 8, 6]); // Use of _.uniq() // method let gfg = _.uniq(y); // Printing the output console.log(gfg); |
Output:
[ 1, 2, 3, 4, 8, 6 ]