Skip to content
Advertisement

How to build a Set of unique Arrays?

I want to add many arrays to a Javascript set, and ensure that only unique arrays are added to the set. However, when I try adding the same array multiple times, it is always added instead of rejected. The .has() method always returns false as well. How do I fix this?

JavaScript

Advertisement

Answer

I’d use a Map instead, indexed by the stringified version of the array:

JavaScript
Advertisement