Remove duplicate value from array

Suprabha - May 14 '21 - - Dev Community

There are multiple ways to filter out duplicates from an array and return only the unique values.

1๏ธโƒฃ Using Set ๐Ÿ”ฅ

What is Set?

Set is a new data object introduced in ES6. A Set is a collection of unique values.

Here,

  • The array is converted toย Setย and all the duplicate elements are automatically removed.
  • The spread syntaxย ...ย is used to include all the elements of theย Setย to a new array.
const arr = ["๐ŸŒผ", "๐ŸŒด", "๐ŸŒน", "๐ŸŒต", "๐Ÿ„", "๐ŸŒน", "๐ŸŒด"];

const filteredArr = [...new Set(arr)];
console.log(filteredArr); //["๐ŸŒผ", "๐ŸŒด", "๐ŸŒน", "๐ŸŒต", "๐Ÿ„"]
Enter fullscreen mode Exit fullscreen mode

Convert Set to an Array using Array.from

You can also use Array.from to convert a Set into an array:

const arr = ["๐ŸŒผ", "๐ŸŒด", "๐ŸŒน", "๐ŸŒต", "๐Ÿ„", "๐ŸŒน", "๐ŸŒด"];

const filteredArr = Array.from(new Set(arr));
console.log(filteredArr); //["๐ŸŒผ", "๐ŸŒด", "๐ŸŒน", "๐ŸŒต", "๐Ÿ„"]
Enter fullscreen mode Exit fullscreen mode

2๏ธโƒฃ Using filter ๐Ÿ•ธ

If the element passes and returns true, it will be included in the filtered array and any element that fails or return false, it will be NOT be in the filtered array.

const arr = ["๐ŸŒผ", "๐ŸŒด", "๐ŸŒน", "๐ŸŒต", "๐Ÿ„", "๐ŸŒน", "๐ŸŒด"];

const filteredArr = arr.filter((item, index) => {
    return arr.indexOf(item) === index;
})
console.log(filteredArr); //["๐ŸŒผ", "๐ŸŒด", "๐ŸŒน", "๐ŸŒต", "๐Ÿ„"]
Enter fullscreen mode Exit fullscreen mode

3๏ธโƒฃ Using forEach Method ๐Ÿš€

Using forEach, you can iterate over the elements in the array and push into the new array if it doesnโ€™t exist in the array.

const arr = ["๐ŸŒผ", "๐ŸŒด", "๐ŸŒน", "๐ŸŒต", "๐Ÿ„", "๐ŸŒน", "๐ŸŒด"];

const filteredArr = (arr) => {
    let uniqueVal = [];
    arr.forEach(el => {
        if(!uniqueVal.includes(el)) {
            uniqueVal.push(el);
        }
    })
    return uniqueVal;
}
console.log(filteredArr(arr)); //["๐ŸŒผ", "๐ŸŒด", "๐ŸŒน", "๐ŸŒต", "๐Ÿ„"]
Enter fullscreen mode Exit fullscreen mode

4๏ธโƒฃ Using Reduce Method ๐Ÿ˜Ž

The reduce method is used to reduce the elements of the array and combine them into a final array based on some reducer function that you pass.

const arr = ["๐ŸŒผ", "๐ŸŒด", "๐ŸŒน", "๐ŸŒต", "๐Ÿ„", "๐ŸŒน", "๐ŸŒด"];

const filteredArr = arr.reduce((acc, curr) => {
    return acc.includes(curr) ? acc : [...acc, curr];
}, [])
console.log(filteredArr(arr)); //["๐ŸŒผ", "๐ŸŒด", "๐ŸŒน", "๐ŸŒต", "๐Ÿ„"]
Enter fullscreen mode Exit fullscreen mode

5๏ธโƒฃ Unique Method to the Array Prototype ๐Ÿ“”

In Javascript the array prototype constructor allows you to add new properties and methods to the Array object.

const arr = ["๐ŸŒผ", "๐ŸŒด", "๐ŸŒน", "๐ŸŒต", "๐Ÿ„", "๐ŸŒน", "๐ŸŒด"];

Array.prototype.filteredArr = function (){
    let arr = [];
    for(let i = 0; i < this.length; i++) {
        let current = this[i];
        if(arr.indexOf(current) < 0 ) arr.push(current);
    }
    return arr;
}
console.log(arr.filteredArr()); //["๐ŸŒผ", "๐ŸŒด", "๐ŸŒน", "๐ŸŒต", "๐Ÿ„"]
Enter fullscreen mode Exit fullscreen mode

Reference ๐Ÿง

๐ŸŒŸ Twitter ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป suprabha.me ๐ŸŒŸ Instagram
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player