
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to find duplicates in an array using set() and filter() methods in JavaScript?
Removing duplicates
To remove duplicates in an array we have many logical methods, but advanced javascript has provided some methods so that the task of removing duplicates has become very simple. Some of those methods are set() and filter(). For better understanding lets' discuss each method individually.
Set()
The important use of the set() method is that it only allows unique values. In other words, it will automatically remove duplicates and makes the task easy for us. The Set() method won't take any logical approach in removing duplicates.
Example
In the following example, the duplicates in the provided array have been removed without any logical approach by using set() method.
<html> <body> <script> var dupNames = ['John', 'Ram', 'Rahim', 'Remo', 'Ram', 'Rahim']; var uniArr = [...new Set(dupNames)]; document.write("Before removing :" +" "+ dupNames); document.write("</br>"); document.write("After using set() method :" +" "+ uniArr); </script> </body> </html>
Output
Before removing : John,Ram,Rahim,Remo,Ram,Rahim After using set() method : John,Ram,Rahim,Remo
filter()
In the following example, using filter() method each element is scrutinized whether it is repeated two or more times. If any element found repeated two or more times then only one of its value is permitted and displayed as shown in the output.
Example
<html> <body> <script> var dupnam = ['John', 'Ram', 'Rahim', 'Remo', 'Ram', 'Rahim']; var x = (dupname) => dupname.filter((v,i) => dupname.indexOf(v) === i) document.write("Before removing : " +" "+ dupname); document.write("</br>"); document.write("After filter() method :" +" "+x(dupname)); </script> </body> </html>
Output
Before removing : John,Ram,Rahim,Remo,Ram,Rahim After filter() method : John,Ram,Rahim,Remo
- Related Articles
- How to use map and filter simultaneously on an array using JavaScript?
- Remove duplicates and map an array in JavaScript
- Removing consecutive duplicates from strings in an array using JavaScript
- How to filter values from an array using the comparator function in JavaScript?
- How to remove an object using filter() in JavaScript?
- Unique sort (removing duplicates and sorting an array) in JavaScript
- Filter array with filter() and includes() in JavaScript
- Filter null from an array in JavaScript?
- Find All Duplicates in an Array in C++
- Find Duplicates of array using bit array in Python
- Find Duplicates of array using bit array in C++
- JavaScript in filter an associative array with another array
- How to filter an array in Java
- Merge and remove duplicates in JavaScript Array
- Using one array to help filter the other in JavaScript
