Fundamental Characteristics of Sets
Unordered Nature
Sets do not maintain any specific order of elements.This distinguishes them from lists or arrays, where the order of elements matters.
Example- List → [10, 20, 30] preserves order.
- Set → {10, 20, 30} has no guaranteed order (may display differently each time).
Hint
Think of a set like a bag of marbles: you know what’s inside, but you don’t care about the order.
Common MistakeAssuming sets will return elements in the same order you added them (order is not guaranteed).
Uniqueness of Elements
- Each element in a set is unique.
- Duplicates are automatically ignored.
- This property makes sets useful for removing duplicates and ensuring clean data
Sets act like a filter: they automatically remove duplicates for you.
Core Operations on Sets
Union
- Combines all elements from two sets, removing duplicates.
- Mathematical Notation: A∪BA \cup BA∪B
{1, 2, 3} ∪ {3, 4, 5} = {1, 2, 3, 4, 5}.
HintThink of union as “everything from both sets”: just merge them and remove duplicates.
Intersection
- Finds common elements between two sets.
- Mathematical Notation: A∩BA \cap BA∩B
{1, 2, 3} ∩ {2, 3, 4} = {2, 3}.
HintThink of intersection as “the overlap”: only what both sets have in common.
Difference
- Identifies elements in one set that are not in another.
- Mathematical Notation: A−BA - BA−B
{1, 2, 3} − {2, 3, 4} = {1}.
Subset and Superset Checks
- Subset: All elements of one set are contained in another.