Foundational Programming Concepts: Sets
Draft 2 minutes readIn this series we look at some foundational programming concepts. This is not a course about how to program in general. Rather we look into specific tools, what they represent, when should we use them, and also situations where they are not the right choice.
What is a Set
A set is a collection of elements that are unique.
From this definition we can figure out some interesting properties:
- Each element in a set will appear only once.
- Inserting an already existing element in a set does nothing.
A set is usually implemented using a binary tree or a hashmap. Although you don’t necessarily need to know what this means, it is important to know that this gives sets the following properties:
- Elements in a set must have a way to determine they are unique!
- It is very fast and cheap to:
- Insert an element into a set.
- Delete an element from a set.
- Verify that a set contains an element.
What does it mean for an element to be unique?
In practice two elements are unique if they are not equal, i.e.: a != b.
This will depend on your language, but broadly speaking if your element is primitive (boolean, number or a string) then it just means that they are different.