DEV Community

Cover image for Remove duplicate value from array
Suprabha
Suprabha

Posted on

53 15

Remove duplicate value from array

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

Top comments (5)

Collapse
 
goutamdas profile image
Goutam Das β€’

Nice comment

Collapse
 
goutamdas profile image
Goutam Das β€’

nice reply

Thread Thread
 
goutamdas profile image
Goutam Das β€’

nice

Collapse
 
valeriavg profile image
Valeria β€’ β€’ Edited

Good stuff!

If you're not planning on doing re-assignment, you can keep arrays (and objects overall const):

const arr=[]
arr.push('value')
Enter fullscreen mode Exit fullscreen mode
Collapse
 
oreoyona profile image
oreoyona β€’

Thank you. I was looking for this.

typescript

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay