JavaScript AcademyProfessional Engineering

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

  1. Use the default arrays. Which value was a duplicate?
  2. Choose [1, "1", NaN, NaN]. How many values does the Set keep?
  3. 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.