How to Remove Duplicate Elements from Array in Javascript
In this tutorial, you will learn how to remove duplicate elements from array in javascript. Arrays generally act like a collection of items. The data type of those items could be similar or could be different.
The tutorial is mainly focused on the string type. The same technique can also work with number type but make sure you do not use toLowerCase()
and toUpperCase()
methods as shown in this tutorial since these methods are only available for strings.
There is no built-in method to remove duplicates items from an array so we have to use other built-in methods and loops in the right sequence to accomplish our goal.
In the following example, we have a global array of fruits. Initially, we will display all fruit names on the screen. Upon click of a button, we will remove duplicates fruit names and update our list. Please have a look over the code example and the steps given below.
HTML & CSS
- We have 2 elements in the HTML file (
button
andul
). - The
innerText
for thebutton
element is“Remove Duplicates”
. - The
ul
element is empty as of now and we will populate it dynamically using javascript. - We have also included our javascript file
script.js
with ascript
tag at the bottom.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <button>Remove Duplicates</button> <ul></ul> <script src="script.js"></script> </body> </html>
Javascript
- We have a global variable
fruits
and it holds an array of strings. - We have the
addFruits()
method which will populate ourul
element with list of fruit names. - In
addFruits()
method, we are simply looping through thefruits
array and creating a template string with a bunch ofli
elements. - We are displaying that list in the
ul
element by using theinnerHTML
property. - We are calling the
addFruits()
method to display our initial fruits list. - We have selected the
button
element using thedocument.querySelector()
method and attached theclick
event listener to it.
Case Sensitive Comparison
- We are using the
Set
constructor to create aSet
of fruits. By default, you cannot have duplicate items in aSet
. We are storing our fruitsSet
in themySet
variable - We are using
Array.from()
method to convertSet
into an array. We are assigning it back to thefruits
variable. - We are calling the
addFruits()
method to update our fruits list.
Case Insensitive Comparison
- We are using the
map()
method to loop through all items in the array. Inside the loop, we are using thetoLowerCase()
method to have all fruit names in the lowercase. - We are using the
filter()
method to remove duplicate items from the array. Inside the loop, we are simply using theindexOf()
method to check if a certain item is already present or not. - We are again using the
map()
method to capitalize the first letter of each fruit name. - Inside the loop, we are using the index of 0 to get the first letter and then using the
toUpperCase()
method to capitalize it. We are using theslice()
method to slice the string from index 1. We are joining the capitalized letter and sliced string by using the addition (+
) operator. - We are calling the
addFruits()
method to update our fruits list.
let fruits = ['Apple', 'Orange', 'Kiwi', 'Grapes', 'Mango', 'kiwi', 'grapes']; function addFruits(){ let template = fruits.map(fruit => `<li>${fruit}</li>`).join('\n'); document.querySelector('ul').innerHTML = template; } addFruits(); document.querySelector('button').addEventListener('click', () =>{ /* let mySet = new Set(fruits); fruits = Array.from(mySet); addFruits(); */ fruits = fruits.map(fruit => fruit.toLowerCase()); fruits = fruits.filter((item, index) => fruits.indexOf(item) == index); fruits = fruits.map(fruit => fruit[0].toUpperCase() + fruit.slice(1)); addFruits(); });