JavaScript — remove duplicates from array

Dirask ❤️ 🙂 💻
2 min readFeb 25, 2021

--

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:

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:

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

--

--