How to Remove Duplicates from an Array
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];23// basic for loop4let finalArray = [];5for (let i = 0; i < startingArray.length; i++) {6 if(finalArray.indexOf(startingArray[i]) === -1) {7 finalArray.push(startingArray[i])8 }9}1011console.log(finalArray) // [2, 3, 4, 5, 6, 34]1213// replace for loop with Array.map14let finalArray2 = [];15startingArray.map(item => {16 if( finalArray2.indexOf(item) === -1 ) {17 finalArray2.push(item)18 }19}2021console.log(finalArray2) // [2, 3, 4, 5, 6, 34]2223// using map with .includes and a ternary operator24const startingArray = [2, 3, 4, 5, 6, 5, 4, 3, 34];25let finalArray3 = [];26startingArray.map(item => {27 finalArray3.includes(item) ? null : finalArray3.push(item);28})2930console.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 temp45startingArray.sort() // [2, 3, 3, 34, 4, 4, 5, 5, 6]67for (let i = 0; i < startingArray.length; i++) {8 if (startingArray[i] !== temp) {9 finalArray.push(startingArray[i])10 temp = startingArray[i]11 }12}1314console.log(finalArray) // [2, 3, 34, 4, 5, 6]
3. Using .filter()
1const startingArray = [2, 3, 4, 5, 6, 5, 4, 3, 34]23const finalArray = startingArray.filter((item, index) => {4 return startingArray.indexOf(item) === index5})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 ? accumulator5 : [...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 = {}34// loops through the array and stores each value as a separate key in the obj5for (let i of startingArray) {6 obj[i] = true7}8// console.log(obj) // {2: true, 3: true, 4: true, 5: true, 6: true, 34: true}910// converts the unique object keys into a new array11let finalArray = Object.keys(obj)1213// *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]23// convert the Array into a Set via the constructor method4const uniques = new Set(startingArray)56// convert the set back into an Array7const uniqueArray = [...uniques]89console.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]23// array --> set --> array4const uniqueArray = [...new Set(startingArray)]56console.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