Remove Duplicates value from Array in javascript


0

In this tutorial we are going to learn, remove duplicate values from an array in javascript

Below a sorted values array, we need to to remove duplicate values from the array.

Examples: 

const array = [1, 1, 1, 3, 3, 2, 2];

// Method 1: Using a Set
const unique = [...new Set(array)];

// Method 2: Array.prototype.reduce
const unique = array.reduce((result, element) => {
  return result.includes(element) ? result : [...result, element];
}, []);

// Method 3: Array.prototype.filter
const unique = array.filter((element, index) => {
  return array.indexOf(element) === index;
});


Like it? Share with your friends!

0
Developer

0 Comments