The Set collection
Since ES6, Set collection has been introduced which is really good and makes our life simple. Set is a collection which will not keep duplicates. It can also take any iterable objects as constructor arguments. In our case, it is an array with duplicated values.
1 | const duplicates = [1, 2, 3, 3, 2, 1]; |
Array.filter function
It is better to know more options, in case of not-so-friendly environment. For instance, Set collection is supported by all the major browsers but few old-schools. Yes, I am looking at you IE! O_o
1 | const duplicates = [1, 2, 3, 3, 2, 1]; |
Please prefer the Set option just for the simple of it!
Tip: We can do the same
.indexOftrick using.map,.foreachas well. But,.filteris more readable among those! ^_^