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
Top comments (6)
Nice solution! Thanks for the tips. 😊🔥
Perfect! Took the words right out of my mouth.
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 namedid
which is to be considered while making theuniqueArray
sourceArray
is basically the original array with duplicates.and can be written in one line if you'd like it that way.
Array of Objects.
You can use YAML with sortKeys, if you mean ANY object.