- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Filtering out the non-unique value to appear only once in JavaScript
We have an array of literals that contains some duplicate values appearing for many times like this −
const arr = [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4];
We are required to write a JavaScript function that takes in this array and pick out all the duplicate entries from the original array and only once.
Therefore, for the above array, the output should be −
const output = [1, 4, 3, 2];
Example
The code for this will be −
const arr = [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4]; const removeDuplicate = arr => { const res = []; for(let i = 0; i < arr.length; i++){ if(arr.indexOf(arr[i]) !== arr.lastIndexOf(arr[i])){ if(!res.includes(arr[i])){ res.push(arr[i]); }; }; }; return res; }; console.log(removeDuplicate(arr));
Output
The output in the console −
[1, 4, 3, 2]
- Related Articles
- Filtering out only null values in JavaScript
- Program to pick out duplicate only once - JavaScript
- Filtering string to contain unique characters in JavaScript
- JavaScript: How to filter out Non-Unique Values from an Array?
- Get count of values that only appear once in a MySQL column?
- Filtering out numerals from string in JavaScript
- Filtering out primes from an array - JavaScript
- Select a value from MySQL database only if it exists only once from a column with duplicate and non-duplicate values
- Array elements that appear more than once?
- Detecting the first non-unique element in array in JavaScript
- Add two array keeping duplicates only once - JavaScript
- Array elements that appear more than once in C?
- Finding the only unique string in an array using JavaScript
- Matching Only Once in Perl
- Number of non-unique characters in a string in JavaScript

Advertisements