<Aaron />

How to Remove Duplicates from an Array

Oct 19, 2019 4 min read

Arrays are one of the most widely used data structures. This article will go over several ways to remove duplicate entries from an array using JavaScript. This is a fairly run-of-the-mill task that someone might have to do, or even something that could come up as an interview question.

Like most things in programing, there are multiple ways to go about solving the problem. The examples below are by no means a complete list.

1. Brute Force

These approaches loop through the input array and use some sort of logic to determine

1const startingArray = [2, 3, 4, 5, 6, 5, 4, 3, 34];
2
3// basic for loop
4let finalArray = [];
5for (let i = 0; i < startingArray.length; i++) {
6 if(finalArray.indexOf(startingArray[i]) === -1) {
7 finalArray.push(startingArray[i])
8 }
9}
10
11console.log(finalArray) // [2, 3, 4, 5, 6, 34]
12
13// replace for loop with Array.map
14let finalArray2 = [];
15startingArray.map(item => {
16 if( finalArray2.indexOf(item) === -1 ) {
17 finalArray2.push(item)
18 }
19}
20
21console.log(finalArray2) // [2, 3, 4, 5, 6, 34]
22
23// using map with .includes and a ternary operator
24const startingArray = [2, 3, 4, 5, 6, 5, 4, 3, 34];
25let finalArray3 = [];
26startingArray.map(item => {
27 finalArray3.includes(item) ? null : finalArray3.push(item);
28})
29
30console.log(finalArray3) // [2, 3, 4, 5, 6, 34]

2. Sort to Remove

1const startingArray = [2, 3, 4, 5, 6, 5, 4, 3, 34]
2const finalArray = []
3let temp
4
5startingArray.sort() // [2, 3, 3, 34, 4, 4, 5, 5, 6]
6
7for (let i = 0; i < startingArray.length; i++) {
8 if (startingArray[i] !== temp) {
9 finalArray.push(startingArray[i])
10 temp = startingArray[i]
11 }
12}
13
14console.log(finalArray) // [2, 3, 34, 4, 5, 6]

3. Using .filter()

1const startingArray = [2, 3, 4, 5, 6, 5, 4, 3, 34]
2
3const finalArray = startingArray.filter((item, index) => {
4 return startingArray.indexOf(item) === index
5})
6console.log(finalArray) // [2, 3, 4, 5, 6, 34]

4. Using .reduce()

This method

1const startingArray = [2, 3, 4, 5, 6, 5, 4, 3, 34]
2const final = startingArray.reduce((accumulator, currentValue) => {
3 return accumulator.includes(currentValue)
4 ? accumulator
5 : [...accumulator, currentValue]
6}, [])

5. Using a Hash Table / Dictionary

This method uses a dictionary to store the values. Objects cannot have duplicate keys so each value can only be stored once. After the values are stored they are then assigned to a new array by using a for...of loop. One possible issue of this approach is that there could be a type conversion in the resultant values.

1const startingArray = [2, 3, 4, 5, 6, 5, 4, 3, 34]
2let obj = {}
3
4// loops through the array and stores each value as a separate key in the obj
5for (let i of startingArray) {
6 obj[i] = true
7}
8// console.log(obj) // {2: true, 3: true, 4: true, 5: true, 6: true, 34: true}
9
10// converts the unique object keys into a new array
11let finalArray = Object.keys(obj)
12
13// *Note the type conversion!!
14console.log(finalArray) // ["2", "3", "4", "5", "6", "34"]

6. Using the Set Data Structure (ES6)

I saved my favorite approach for last. This way utilizes the Set data structure. Sets are a new addition that was introduced to JavaScript in ES6. They are perfect for this problem because they cannot have duplicate values by default. When you convert an array to a set any duplicate values are stripped away.

1const startingArray = [2, 3, 4, 5, 6, 5, 4, 3, 34]
2
3// convert the Array into a Set via the constructor method
4const uniques = new Set(startingArray)
5
6// convert the set back into an Array
7const uniqueArray = [...uniques]
8
9console.log(uniqueArray) // [2, 3, 4, 5, 6, 34]

You can also do this in one line of code using the spread operator, but I find the more verbose method above easier to read.

1const startingArray = [2, 3, 4, 5, 6, 5, 4, 3, 34]
2
3// array --> set --> array
4const uniqueArray = [...new Set(startingArray)]
5
6console.log(uniqueArray) // [2, 3, 4, 5, 6, 34]

I had fun going through some of the many different methods of solving the problem of removing duplicate values from an array. Hopefully this article was helpful for you as well :)

Find a mistake? Help me fix it by submitting a pull request.

← back to all posts