date | tags | |
---|---|---|
2021-05-05T19:27 |
|
A set is a collection in which:
- Each item is distinct. There are no duplicates
- There's no sense of order. You cannot sort a set.
Operation | Syntax | Example |
---|---|---|
Union | ||
Intersection | ||
Set difference | ||
Symmetric difference |
- Copy set
$A$ into set$X$ - Add all values of set
$B$ into set$X$ , skipping duplicates
-
New empty set
$X$ -
Foreach value in
$A$ - If it's not in
$B$ , then add to$X$
- If it's not in
-
Foreach value in
$B$ - If it's not in
$A$ , then add to$X$
- If it's not in
- Copy set
$A$ into set$X$ - Remove all values of set
$B$ from set$X$ , skipping values that don't exist.
Can be calculated using the previous operations:
For example:
Math | .NET HashSet<T> |
---|---|
Union | .UnionWith(IEnumerable<T>) |
Intersection | .IntersectWith(IEnumerable<T>) |
Set difference | .Except(IEnumerable<T>) |
Symmetric difference | .SymmetricExceptWith(IEnumerable<T>) |
- Robert Horvick (2020, November 2). Algorithms and Data Structures - Part 2 [Course]. Pluralsight. https://www.pluralsight.com/courses/algorithms-data-structures-part-two