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 

Output

The output in the console −

[1, 4, 3, 2]
Updated on: 2020-10-15T09:17:46+05:30

235 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements