JavaScript - remove duplicates from array
Dirask-React
Posted on February 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:
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]
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]
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
Posted on February 25, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
October 16, 2024