Sets

► Understanding Sets

Sets are unordered collections of unique elements. They automatically remove duplicates and are perfect for membership testing, removing duplicates, and mathematical set operations.

► Real-World Analogy

Think of sets like a bag of unique marbles:
» If you try to add a marble that’s already in the bag, nothing happens
» You can’t have two identical marbles (no duplicates)
» The order doesn’t matter – you just have a collection of unique items
» You can combine bags (union) or find common marbles (intersection)
» Perfect for answering “Is this marble in the bag?”

► Key Concepts

✓ Unique elements: No duplicates allowed
✓ Unordered: Items have no specific position
✓ Mutable: Can add/remove elements
✓ Fast membership testing: O(1) average time
✓ Mathematical operations: union, intersection, difference

■ Code Examples