Are you a web developer? Are you a programmer? Then you would be familiar with JavaScript & its different in-built functions, methods, etc for different implementations, problems & purposes.
One of those widely asked problems in interviews in any programming language (we will talk about JavaScript here) is finding unique elements in an array or removing duplicates from an array.
So, this article will teach you three simple methods to execute it from scratch. Here we go,
Before we start, just a suggestion, use Visual Studio Code as it is the best editor for everything, specifically for web development-related stuff. Let’s look over setup-related stuff that one should do. So, if you run any JavaScript program in it or if you are a beginner in it, you should do the following things to make things smooth,
First of all, we would declare an array with duplicate elements namely animals and an array with all unique elements namely unique in all methods we will discuss.
The Set object stores unique values of any type, whether primitive values or object references.
Here, to remove duplicates, we convert the animal array to a Set. The new Set will implicitly remove duplicate elements. Then we will convert this set back to the unique array.
You might be wondering what these … or three dots or technically spread syntax are in JavaScript. Well, it takes in an iterable entity like arrays or lists, etc, and expands it into individual elements.
- Install this extension in Visual Studio Code.
- To run your program, just “Open New Terminal” and then write node <filename>. Make sure you have installed node.js on your system.
- For more reference, you can visit here.
First of all, we would declare an array with duplicate elements namely animals and an array with all unique elements namely unique in all methods we will discuss.
Using Sets
The Set object stores unique values of any type, whether primitive values or object references.
Here, to remove duplicates, we convert the animal array to a Set. The new Set will implicitly remove duplicate elements. Then we will convert this set back to the unique array.
You might be wondering what these … or three dots or technically spread syntax are in JavaScript. Well, it takes in an iterable entity like arrays or lists, etc, and expands it into individual elements.
- // Remove duplicates in an array using Set.
- let animals = ["Lion", "Rabbit", "Mouse", "Monkey", "Lion","Ape"]
- let unique = [...new Set(animals)]
- console.log(unique)
- Output: ['Lion', 'Rabbit', 'Mouse', 'Monkey', 'Ape']
Using Filter
To under this method, let’s understand what & why of methods that have been used,
indexOf()
This method returns the index of the first occurrence of an element in an array. The logic we used that the duplicate item is the item whose index is different from its indexOf() value, from here what we returned that the element whose current index and indexOf() value is equal.
filter()
filter()

Ramakrishnan RPosted Jul 20, 2021, 4:24 AM
Excellent, useful!