# Set Operations – How to Create and Modify Sets

A set is a container and defined as a (mathematical) set of type T (set< T >), where T may be an enumeration type or one of the primitive types; it may even be a node or edge or graph type, then we speak of storages. Sets only store unique values, i.e. if one would add the same value (or element) multiple times to a set only one unique entry would be stored in the set.

For handling sets there are different opreations or calls. This article covers the following set operations:

## Query method calls

The query method calls on sets are:

Method CallDescription
.size()returns the number of elements in the set, as int
.empty()returns whether the set is empty, as boolean
.peek(num)returns the element which comes at position num:int in the sequence of enumeration, as T for set; the higher the number, the longer retrieval takes
.asArray()returns an array of the set content, as array for set, in enumeration order

The operator x in s denotes set membership, i.e. whether the set s contains the
given element x, and returns the result as
boolean (true or false). Furthermore, the container may
be iterated over with a
for loop, e.g. for(x:string in s)…, looping over the string set s with the parameter x

## Update method calls

The update method calls on are:

Method CallDescription
s.rem(v) Set removal: removes the value v from the set s.
s.clear()Set clearing: removes all values from the set s.

## Binary and comparative set operators

Boolean set operators are:

Binary OperatorDescription
| Set union (contained in resulting set as soon as contained in one of the sets)
&Set intersection (contained in resulting set only if contained in both of the sets)
\Set difference (contained in resulting set if contained in left but not right set)

Operators for set comparisons are:

Binary OperatorDescription
A == B True, if A and B are identical.
A != B True, if A and B are not identical.
A < B True, if A is a subset of B, but A and B are not identical.
A > B True, if A is a superset of B, but A and B are not identical.
A <= B True, if A is a subset of B or A and B are identical.
A >= B True, if A is a superset of B or A and B are identical.

## Copy operator

To copy a set use copy(set<T>):set<T> as shown in the example below.

## Example

An example explaining some operations.

```function setExample(ref si:set<int>) : boolean
{
def ref s:set<int> = set<int>{};  //create an empty integer set s
def ref s1:set<int> = set<int>{};  //create an empty integer set s1
s.add(3);                     // Add 3 to s { 1, 2, 3 }, s.size()==3
s1 = copy(s);                 // Copy s to s1, s1 = { 1, 2, 3 }
s.rem(2);                     // Remove 2 from s { 1, 3 }
for(v:int in s) {             // Loop over all elements in the set