Set
- distinct elements
- unordered
- search, insert, delete -
O(1)
Initialize
- Python
s = {10, 20}
s = set([10, 20])
s = set()
Insert
- Python
s.add(30)
s.update([40, 50]) # adds 40 and 50
Delete
- Python
s.discard(30)
s.remove(30)
s.clear()
danger
s.remove(30) will throw error if item is not present
Others
- Python
len(s)
if 20 in s
#union
s1 | s2
s1.union(s2)
# intersection
s1 & s2
s1.intersection(s2)
# difference
s1 - s2
s1.difference(s2)
# symmetric difference
# elements in s1 and s2 but not common
s1 ^ s2
s1.symmetricdifference(s2)
s1.isdisjoint(s2)
# subset
s1 <= s2
s1.issubset(s2)