In this article, you will learn how to remove duplicate words from an array in JavaScript.
That’s the array of duplicate words that we have:
let duplicate_words = [
"camera",
"signature",
"camera",
"guitar",
"guitar",
"camera",
];
By default, a set cannot have duplicate elements. So, we can remove duplicate words from an array by converting it to a set.
let words_set = new Set(duplicate_words);
If we log the set to the console, we will get:
console.log(words_set); // Set(3) {"camera", "signature", "guitar"}
We need it as an array, so we will convert the set back to an array using the spread operator.
let words = [...words_set];
Now, log the words to the console.
console.log(words); // ["camera", "signature", "guitar"]
The duplicate words have been removed from the array successfully!