Learning object 31 of 100
Set operations explorer
Turn two arrays into Sets and see duplicates disappear, plus their union, intersection, differences and subset check.
Loading…
What it shows
A Set stores each value once, using === comparison (except that NaN equals NaN). Union, intersection and difference are built by spreading Sets into arrays and filtering with has(), which is fast even for large collections.
Guided experiment
- Use the default arrays. Which value was a duplicate?
- Choose
[1, "1", NaN, NaN]. How many values does the Set keep? - Make B a subset of A and check the last row.
Predict
What is new Set([1, 1, 2]).size?
Related
Lessons: Lesson 45: Sets, Maps and modern collection patterns
Projects: Project 9: Shopping Cart Price Calculator
Source: web/assets/js/objects/object-031.js in the companion repository.