DEV Community

Cover image for JavaScript - remove duplicates from array
Dirask-React
Dirask-React

Posted on • Originally published at dirask.com

JavaScript - remove duplicates from array

Hello Coders! 👋 😊

In this short article, I would like to show you how to remove duplicated items from an array in JavaScript.

Before we start, I would highly recommend you to check out runnable example for the solution on our website:
JavaScript - remove duplicates from array

Quick solution

In this quick solution, I've used the built-in filter() function which has been added to improve functional programming.

If an index with the same value is found on another position, it won't be saved (in other words, it saves only those that occurred for the first time, it does not take into account the next ones).

Practical example:

const array = [1, 2, 3, 1, 1, 2, 2, 3, 3];
const result = array.filter((item, index, array) => array.indexOf(item) === index);

console.log(JSON.stringify(result)); // [1,2,3]
Enter fullscreen mode Exit fullscreen mode

You can run this example here

Iterative example

In this approach, I've used a blocker object that represents a map of elements that have already occurred. The for loop iterates only once over all elements adding to this map and if an element has already appeared, it won't add it again.

This solution is more optimal because it has lower computational complexity. 📉✅

Practical example:

const removeDuplicates = (array) => {
    const result = [];
    const blocker = {}; // prevents against item duplication
    for (const item of array) {
        if (blocker.hasOwnProperty(item)) {
            continue;
        }
        blocker[item] = true;
        result.push(item);
    }
    return result;
};

// Usage example:

const array = [1, 2, 3, 1, 1, 2, 2, 3, 3];
const uniqueItems = removeDuplicates(array);

console.log(JSON.stringify(uniqueItems)); // [1,2,3]
Enter fullscreen mode Exit fullscreen mode

You can run this example here

Thank you for your time! I hope you like the solution. 😊
If you have any questions, drop a comment below. 💬

See you in upcoming posts! 🔥🔜


Write to us! ✉

If you have any problem to solve or questions that no one can answer related to a React or JavaScript topic, or you're looking for a mentoring write to us on dirask.com -> Questions

Top comments (6)

Collapse
 
ndom91 profile image
Nico Domino • Edited
const set = new Set([1, 2, 3, 1, 1, 2, 2, 3, 3])
console.log(set)
// Set {1, 2, 3}
console.log(Array.from(set))
// [ 1, 2, 3 ]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
diraskreact profile image
Dirask-React

Nice solution! Thanks for the tips. 😊🔥

Collapse
 
marklchaves profile image
mark l chaves

Perfect! Took the words right out of my mouth.

Collapse
 
kanhalelor profile image
robSyntax
const arr = [1, 2, 3, 1, 1, 2, 2, 3, 3];

const newArr = [...new Set(arr)];

console.log(newArr);
// [ 1, 2, 3 ]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
barelyhuman profile image
Reaper

I like this one and I think it's a bit faster. Uses Sets as well.
and is obviously a lot more useful in real life since you'll be using objects and keys more than just numbers.

online snippet

The snippet considers item to be have a key named id which is to be considered while making the uniqueArray
sourceArray is basically the original array with duplicates.

sourceArray.reduce((acc,item)=>{
    if(!acc.has(item.id)){
        uniqueArray.push(item);
        acc.add(item.id);
    }

    return acc;
},new Set());

Enter fullscreen mode Exit fullscreen mode

and can be written in one line if you'd like it that way.

sourceArray.reduce((acc,item)=>(!acc.has(item.id)?(uniqueArray.push(item) && acc.add(item.id) && acc):acc),new Set());
Enter fullscreen mode Exit fullscreen mode
Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

Array of Objects.

import str from 'json-stable-stringify'

console.log(arrObjDupl.filter(
  (a, i, arr) => {
    const strA = str(a)
    return arr.findIndex((b) => str(b) === strA) === i
  }
))

// Or more efficiently
const tmpArr = arrObjDupliStr.map((a) => str(a))

console.log(arrObjDupl.filter(
  (_, i) => tmpArr.indexOf(tmpArr[i]) === i
))
Enter fullscreen mode Exit fullscreen mode

You can use YAML with sortKeys, if you mean ANY object.