Set operations

We can perform the following operations on sets:

  • Union: Given two sets, this returns a new set with the elements from both given sets
  • Intersection: Given two sets, this returns a new set with the elements that exist in both sets
  • Difference: Given two sets, this returns a new set with all elements that exist in the first set and do not exist in the second set
  • Subset: This confirms whether a given set is a subset of another set

Set union

The mathematic concept of union is that the union of sets A and B, denoted by Set union, is the set defined as:

Set union

Get Learning JavaScript Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.