0% found this document useful (0 votes)
21 views

Remove Duplicates From An Array-Js

This document discusses three methods for removing duplicate elements from an array in JavaScript: 1) Using a Set, which implicitly removes duplicates, 2) Using indexOf() and filter() to only keep elements where the index matches the first occurrence, and 3) Using forEach() and includes() to only add elements not already in the unique array.

Uploaded by

minal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Remove Duplicates From An Array-Js

This document discusses three methods for removing duplicate elements from an array in JavaScript: 1) Using a Set, which implicitly removes duplicates, 2) Using indexOf() and filter() to only keep elements where the index matches the first occurrence, and 3) Using forEach() and includes() to only add elements not already in the unique array.

Uploaded by

minal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Remove Duplicates from an Array

Summary: in this tutorial, you will learn how to remove duplicates from an array
in JavaScript.

1) Remove duplicates from an array using a Set


A Set is a collection of unique values. To remove duplicates from an array:

 First, convert an array of duplicates to a Set. The new Set will implicitly


remove duplicate elements.
 Then, convert the set back to an array.

The following example uses a Set to remove duplicates from an array:


let chars = ['A', 'B', 'A', 'C', 'B'];
let uniqueChars = [...new Set(chars)];

console.log(uniqueChars);
Output:

[ 'A', 'B', 'C' ]

2) Remove duplicates from an array


using indexOf() and filter() methods
The indexOf() method returns the index of the first occurrence of an element in an
array. For example:
let chars = ['A', 'B', 'A', 'C', 'B'];
chars.indexOf('B');
Output:

1
The duplicate item is the item whose its index is different from its indexOf() value:
let chars = ['A', 'B', 'A', 'C', 'B'];

chars.forEach((c, index) => {


console.log(`${c} - ${index} - ${chars.indexOf(c)}`);
});
Output:

A - 0 - 0
B - 1 - 1
A - 2 - 0
C - 3 - 3
B - 4 - 1
To remove the duplicates, you use the filter() method to include only elements
whose indexes match their indexOf values:
let chars = ['A', 'B', 'A', 'C', 'B'];
let uniqueChars = chars.filter((c, index) => {
return chars.indexOf(c) === index;
});

console.log(uniqueChars);
Output:

[ 'A', 'B', 'C' ]


To find the duplicate values, you just need to reverse the condition:

let chars = ['A', 'B', 'A', 'C', 'B'];

let dupChars = chars.filter((c, index) => {


return chars.indexOf(c) !== index;
});

console.log(dupChars);
Output:

[ 'A', 'B' ]

3) Remove duplicates from an array using forEach() and include()


The include() returns true if an element is in an array or false if it is not.
The following example iterates over elements of an array and adds to a new array
only elements that are not already there:

let chars = ['A', 'B', 'A', 'C', 'B'];

let uniqueChars = [];


chars.forEach((c) => {
if (!uniqueChars.includes(c)) {
uniqueChars.push(c);
}
});

console.log(uniqueChars);
Output:

[ 'A', 'B', 'C' ]


In this tutorial, you have learned some techniques to to remove duplicates from an
array in JavaScript.

You might also like