A set implementation that uses a hash table to keep track of the unique elements.
Pros | Cons |
---|---|
Best amortized times for inserts/deletes | Bad hashing implementations or sizing can lead to linear times |
No ordering of elements |
s = set()
s.add(10) # {10}
s.add(20) # {10, 20}
s.add(10) # {10}s.remove(10) # {20}
Set<Integer> s = new HashSet<>();
s.add(10); // {10}
s.add(20); // {10, 20}
s.add(10); // {10, 20}s.remove(10); // {20}
std:: unordered_set <int> s;
s.insert(10); // {10}
s.insert(20): // {10, 20}
s.insert(10); // {10, 20}s.erase(10); // {20}
Operations | Worst | Average |
---|---|---|
Delete | O(n) | O(1) |
Insert | O(n) | O(1) |
Search | O(n) | O(1) |