Sets or Hashsets¶
Sets or Hashsets are a collection of unique elements. They are implemented using a hash table, which allows for fast lookups, insertions, and deletions. The time complexity for these operations is O(1) on average.
Hashtables use a hash function to compute an index into an array of buckets or slots, from which the desired value can be found. When a collision occurs (i.e., when two different keys hash to the same index), the hash table uses a method to resolve the collision, such as chaining or open addressing.
Example:
word: "hello"
Hash function: hash("hello") -> 12345
Index: 12345 % 100 = 45
The word "hello" would be stored in the bucket at index 45. If another word, say "world", also hashes to index 45, it would be stored in the same bucket, and the hash table would need to resolve the collision to ensure both words can be accessed efficiently.
| index | bucket |
|---|---|
| 0 | |
| 1 | |
| ... | |
| 45 | "hello", "world" |
Values must be hashable, commonly immutable types like strings, integers, and tuples. Mutable types like lists and dictionaries cannot be used as elements in a set because they cannot be hashed.
Unique elements in a list
Adding an element to a set - O(1)
Removing an element from a set - O(1)
Removing an element from a set, while ignoring errors
Removing first stored element from a set
Clearing a set
Check if an element is in the set - O(1)
Union of two sets
Intersection of two sets
Update set1 with the intersection of set1 and set2
Difference of two sets
Symmetric difference of two sets
are all set1 elements present in set2?
are all set2 elements present in set1?