JavaScript – Is Set better than Array?

What are differences between Set and Array?

  • Arrays can have duplicate values but Sets can’t
  • Array is ordered by index. Set uses keys and elements are iterable in the order of insertion

When is Set better than Array?

Removing duplicate items

let aSet = new Set(anArray);        // ['A', 'B', 'B', 'C', 'D', 'B', 'C'] => {"A", "B", "C", "D"}
let aSet = [...new Set(anArray)];   // ['A', 'B', 'B', 'C', 'D', 'B', 'C'] => ["A", "B", "C", "D"]

Be the first to comment

Leave a Reply

Your email address will not be published.


*