What is the fastest way to remove duplicate values from a JavaScript array?
Removing duplicate values from a JavaScript array of primitive values quickly using […new Set(array)] keeps only unique elements in their original order. A Set automatically drops repeated values, and the spread operator … turns it right back into a regular array.~~
How Set Removes Array Duplicates
-
Automatic uniqueness: A Set stores each distinct value only once as JavaScript loops through the array.
-
Preserves order: The first appearance of each value stays in its original position.
-
Ready as an array: Spreading new Set(…) with … gives you a standard array with .length, .map(), and .filter().
Example: Removing Duplicate Values from an Array
JavaScript
const tags = ["js", "html", "js", "css", "html", "react"];
const uniqueTags = [...new Set(tags)];
console.log(uniqueTags); // ["js", "html", "css", "react"]
You must log in or register to comment.
