Let’s say you have an array of numbers, which has duplicate items and, you want to clean it up so that it only holds unique value. To remove duplicate items, we can use the
Set data structure. The
Set object allows you to create a collection of unique values and does not permit the insertion of duplicate items.
Let’s look at the following example
let array = [1, 1, 3, 4, 3, 5, 6, 7, 8, 9, 0, 11, 12, 3]
array variable holds a lot of duplicate items and only 11 unique items. To filter out duplicate items, we need to pass the
array variable as input to the
let array = [1, 1, 3, 4, 3, 5, 6, 7, 8, 9, 0, 11, 12, 3] let unique = new Set(array)
That’s it, if we now log the value of the
unique variable we can see it now holds unique values only.
Learn how to convert set to array again.