Working with sets

Math

A set is a well-defined collection of distinct objects. The objects of a set are called its elements.

By well-defined, we mean that there is a rule that enables us to determine whether a given object is an element of the set.

If a set has no elements, it is called the empty set, or null set, and is denoted by the symbol ∅.

D = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

This method of denoting a set is called the roster method. A second way to denote a set is to use set-builder notation, where the set D of digits is written as

D = { x | x is a digit} Read as "D is the set of all x such that x is a digit."

E = { x | x is an even digit } = {0, 2, 4, 6, 8}

O = { x | x is an odd digit } = {1, 3, 5, 7, 9}

The numbers in the set
            ⎧   ⎜     a                                    ⎫
            ⎨ x ⎜ x = - , where a,b are integers and b ≠ 0 ⎬, are called rational numbers.
            ⎩   ⎜     b                                    ⎭
            

Because the elements of a set are distinct, we never repeat elements. We would never write {1,2,3,2}, the correct listing is {1,2,3}.

Because a set is a collection, the order in which the elements are listed is immaterial. {1,2,3}, {3,1,2}, {3,2,1} all represents the same set.

If every element of a set A is also an element of a set B, then we say that A is a subset of B and write A ⊆ B.

If two sets A and B have the same elements, then we say that A equals B and write A = B.

For example, {1,2,3} ⊆ {1,2,3,4,5} and {1,2,3} = {3,1,2}

If A and B are sets, the intersection of A with B, denoted A ∩ B, is the set consisting of elements that belong to both A and B.

The union of A with B, denoted A ∪ B, is the set consisting of elements that belong to either A or B, or both.

Let A = {1, 3, 5, 8}, B = {3, 5, 7}, and C = {2, 4, 6, 8}.

Find:

(a) A ∩ B
(b) A ∪ B
(c) B ∩ (A ∪ C)

Usually, in working with sets, we designate a universal set U, the set consisting of all the elements that we wish to consider. Once a universal set has been designated, we can consider elements of the universal set not found in a given set.

If A is a set, the complement of A, denoted Ā, is the set consisting of all the elements in the universal set that are not in A.

If the universal set is U = {1, 2, 3, 4, 5, 6, 7, 8, 9} and if A = {1, 3, 5, 7, 9}, then Ā = {2, 4, 6, 8}.

It follows from the definition of complement that A ∪ Ā = U and A ∩ Ā = ∅.

U = universal set = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
A = {1, 3, 4, 5, 9},
B = {2, 4, 6, 7, 8},
C = {1, 3, 4, 6}

Find:
A (complement of A)
C (complement of C)
A ∩ B (complement of intersect of A and B)
B ∪ C (complement of union of B and C)
AB (union of complement of A and complement of B)
BC (intersect of complement of B and complement of C)

It is often helpful to draw pictures of sets. Such pictures, called Venn diagrams, represent sets as circles enclosed in a rectangle, which represents the universal set. Such diagrams often help us to visualize various relationships among sets.

If we know that A ⊆ B, we might use the Venn diagram in Figure 1(a).

If we know that A and B have no elements in common, that is, if A ∩ B = ∅, we might use the Venn diagram in Figure 2(b). The sets A and B in Figure 2(b) are said to be disjoint.

Universal set A B C Universal set A B (a) subset A ⊆ B Universal set A B (b) A ∩ B = ∅ disjoint sets
Figure 1
Universal set A
Figure 2

JavaScript

In JavaScript we use Set object to create a set.

The constructor accepts any iterable as a parameter to create a set from:

new Set([iterable]);

let D = new Set([1,2,3]);

Because the elements of a set are distinct, we never repeat elements.