DEV Community

Cover image for JS Program to Remove Duplicates From Array
hannaA
hannaA

Posted on

JS Program to Remove Duplicates From Array

solution1

let array = ['1', '2', '3', '2', '3'];

let uniqueArray = [];
array.forEach((c) => {
    if (!uniqueArray.includes(c)) {
        uniqueArray.push(c);
    }
});

console.log(uniqueArray);
Enter fullscreen mode Exit fullscreen mode

solution2

function getUnique(arr){

    let uniqueArr = [];

    //loop through array 
    for(let i of arr) {
        if(uniqueArr.indexOf(i) === -1) {
            uniqueArr.push(i);
        }
    }
    console.log(uniqueArr);
}
const array = [1,2,3,2,3];

Enter fullscreen mode Exit fullscreen mode

solution3

let array = ['1', '2', '3', '2', '3'];
let uniqueArray = [...new Set(array)];

console.log(uniqueArray);
Enter fullscreen mode Exit fullscreen mode

Top comments (8)

Collapse
 
miketalbot profile image
Mike Talbot ⭐ • Edited

Both of these solutions are exponentially slow due to the need to search the growing unique array (includes and indexOf do a linear search). You can do the unique array in O(N) using a Set, Map or in this case a PoJo. The one liner using Set is:

    const uniqueArr = [...new Set(array)]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

To add context here for learning purposes, a POJO is an object that only contains data (as opposed to methods or internal state), most JavaScript codebases consider objects created using curly braces {} to be POJOs, however, more strict codebases sometimes create POJOs by calling Object.create(null) to avoid inheriting from the built-in Object class.

JavaScript Maps are an alternative to POJOs for storing data because they neither inherit keys from the Object class.

A downside is that objects are generally easier to work with than maps cause not all JS functions, frameworks, and libs support maps.
i.e. JSON.stringify() function doesn't serialize maps by default:

const map = new Map([['score', 256, 'stage', 3]]);
JSON.stringify(map); // '{}'
Enter fullscreen mode Exit fullscreen mode
Collapse
 
hannaha88 profile image
hannaA

Best friends are options kk thanks

Collapse
 
ivan_jrmc profile image
Ivan Jeremic • Edited

People should better lern how to use JavaScript Set() instead of using arrays which need to be unique.

Collapse
 
hannaha88 profile image
hannaA

So which solution are you using to Remove Duplicates From Array?

Collapse
 
ivan_jrmc profile image
Ivan Jeremic • Edited

Set() does not allow duplicates in the first place so there is no need for me to remove duplicates just use Set from the beginning when it need to be unique.

Thread Thread
 
hannaha88 profile image
hannaA

It's a good suggestion if you care about the performance.

Collapse
 
bap_33 profile image
bap • Edited

The best solution of above is solution3, but i suggest a new solution if didn't know about Set
Create a new object and create a new key with value true if you loop in array and it not existed, so if it not you will get the undefined value