# 2.4 Sets

A set is a collection of items, and is a collection of a particular form. The items appearing in a set are referred to as the **elements** (or **members**) of the set. Examples of sets mentioned earlier are: *Int*, *Char* and *Bool*.

A **set** is a collection in which repetition is not significant, nor is the order in which the items are given. For example, the supermarket might sell its own brand of Wheat Flakes in three sizes: large, medium and small. In a situation where we are interested in the types of product that are available, we are interested in the set of sizes. This set of sizes may be written as:

{large, medium, small}.

We might choose to name the set:

Sizes= {large, medium, small}.

The order in which the elements are listed is of no significance, so we might equally well have written *Sizes* = {small, medium, large}.

Notice the use of curly brackets {and} here. These are used as a signal that the collection is a set (as distinct from some other form of collection, such as a sequence). Note too the commas used to separate the elements.

We would not usually give any repetitions when writing a set, but if one were to do so, repetition of an element would be of no significance. We could write a set as

{large, small, large, medium, small}

but this set is the same as *Sizes* and has three members, not five!

We write *a* ∈ *X* to mean that the item *a* is a member of the set *X* (we say “*a* is in the set *X*”, or just “*a* is in *X*”). So, for example, we might write small ∈ *Sizes*.

It was easy to write out the set *Sizes* in full because it has only three elements. Imagine having to write out the set of all barcodes used at a large supermarket. It would not be practicable! We have another notation for such sets, that is sets whose members can be precisely described. Suppose that, a little unrealistically, a store (YtoZ Groceries) stocks just 90000 items, which happen to have barcodes that are consecutive integers between 10000 and 99999. Then the set of barcodes used by the store may be written:

*YtoZBarcodes* = { *n* ∈ *Int* : 10000 ≤ *n* and *n* ≤ 99999}.

A rather literal reading of this notation would be: “*YtoZBarcodes* is the set of values, *n*, in the set of integers, such that *n* is greater than or equal to 10000 and less than or equal to 99999.”. (We read the first curly bracket as “the set of” and the colon as “such that”.) More casually, we might say: “*YtoZBarcodes* is the set of integers between 10000 and 99999, inclusive.”. (The word inclusive makes it clear that the numbers 10000 and 99999 are each included in the set.)

Alternatively, the condition 10000 ≤ *n* and *n* ≤ 99999 can be written as 10000 ≤ *n* ≤ 99999.

A set need not have any members. (The set {*n* ∈ *Int : n* > 100 and *n* < 50} is an example of a set with no members.) A set with no members is called an **empty set**. We will write this as { }.

Sometimes we need to say how many elements there are in a set. For example, the set *Sizes*, as above, has 3 elements, while the set *YtoZBarcodes* has 90000 members. The number of elements in a set is called the **cardinality** of the set. The cardinality of the empty set is 0. (We shall only talk about the cardinality of a finite set. The set *Int*, for example, does not have a finite number of elements.) If the members of a set have been written out with some elements repeated, then remember to count each repeated element only once when finding the set's cardinality. In other texts, you may see the symbol ∅ used to denote an empty set.

## Activity 5

(a) Write out in full the set {

*n*∈*Int*: 5 ≤*n*and*n*< 9}.(b) Write out in full the set {

*c*∈*Char*: the ASCII code of*c*is between 40 and 43, inclusive}.(c) Which of the following is true?

(i) 7 ∈

*Int.*(ii) ‘7’ ∈

*Int*.

(d) Let

*Lowercase*= {*c*∈*Char*: the ASCII code of*c*is strictly greater than 96 and strictly less than 123}.(i) Is ‘C’ ∈

*Lowercase*true?(ii) What is the cardinality of the set

*Lowercase*?

(e) Let

*A*be the set of characters that appear in the string “aardvark”. What is the cardinality of*A*?

### Discussion

(a) This set contains those integers that are both greater than or equal to 5, and strictly less than 9. We can write this set in full as {5, 6, 7, 8}. (Alternatively, it could be written with the elements in some other order, such as {8, 7, 5, 6}.)

(b) {‘(‘, ‘)’, ‘*’, ‘+’}. (Remember that we write characters in single inverted commas.)

(c) 7 is an integer, but ‘7’ is a character. So 7 ∈

*Int*is true, but ‘7’ ∈*Int*is false.(d) (i) The ASCII code of ‘C’ is 67 and so ‘C’ is not a member of the set

*Lowercase*. So ‘C’ ∈*Lowercase*is false.(ii) There are 26 integers which are strictly greater than 96 and strictly less than 123. So

*Lowercase*has cardinality 26.(e) The set of characters that appear in the string “aardvark” is: {‘a’, ‘r’, ‘d’, ‘v’, ‘k’}. This set has cardinality 5.