
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I make an array with unique elements (remove duplicates) - JavaScript?
Let’s say the following is our array with duplicate elements −
var duplicateNumbers = [10, 20, 100, 40, 20, 10, 100, 1000];
We want the output to be −
[10, 20, 100, 40, 1000];
To display only the unique elements, use the concept of filter.
Example
Following is the code −
var duplicateNumbers = [10, 20, 100, 40, 20, 10, 100, 1000]; console.log("With Duplicates Values="); console.log(duplicateNumbers); var noDuplicateNumbersArray = duplicateNumbers.filter(function (value, index, array) { return array.indexOf(value) === index; } ); console.log("Without Duplicates Values=") console.log(noDuplicateNumbersArray);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo234.js.
Output
The output is as follows −
PS C:\Users\Amit\JavaScript-code> node demo234.js With Duplicates Values= [ 10, 20, 100, 40, 20, 10, 100, 1000 ] Without Duplicates Values= [ 10, 20, 100, 40, 1000 ]
- Related Questions & Answers
- How do I recursively remove consecutive duplicate elements from an array?
- Remove duplicates and map an array in JavaScript
- Unique sort (removing duplicates and sorting an array) in JavaScript
- How do I remove a particular element from an array in JavaScript
- Remove array duplicates by property - JavaScript
- Remove duplicates from array with URL values in JavaScript
- Counting unique elements in an array in JavaScript
- Merge and remove duplicates in JavaScript Array
- How do I empty an array in JavaScript?
- Remove duplicates from an array keeping its length same in JavaScript
- Remove duplicates from a array of objects JavaScript
- How do I select elements inside an iframe with Xpath?
- How do I push elements to an existing array in MongoDB?
- How to remove duplicate elements from an array in JavaScript?
- Java Program to Remove Duplicates from an Array List
Advertisements