What is a Set?
A Set is an unordered collection of unique elements. Unlike lists or arrays, sets do not allow duplicate values. They are useful in situations where you need to maintain a group of items without repetition and perform operations like union, intersection, and difference.
Why Use Sets?
- To remove duplicate entries from a collection.
- To efficiently perform membership tests (e.g., "is this item in the group?").
- To perform mathematical set operations.
Example 1: Creating a Set
Let’s start by creating a simple set with a few elements.
SET fruits = {"apple", "banana", "cherry", "apple"}
PRINT fruits
Output:
{"apple", "banana", "cherry"}
Explanation:
Notice how the set automatically removed the duplicate "apple". This is one of the core features of sets — uniqueness.
Example 2: Adding and Removing Elements
You can add or remove items from a set using standard operations.
SET numbers = {1, 2, 3}
ADD 4 TO numbers
REMOVE 2 FROM numbers
PRINT numbers
Output:
{1, 3, 4}
Question:
What happens if you try to remove an element that doesn't exist in the set?
Answer:
In many languages or pseudocode standards, this results in either no change or an error depending on how the "remove" operation is defined. It's common to check for the item before attempting removal.
Example 3: Set Operations
Let’s explore some common set operations: union, intersection, and difference.
SET A = {1, 2, 3}
SET B = {3, 4, 5}
SET unionAB = A UNION B
SET intersectAB = A INTERSECT B
SET diffAB = A DIFFERENCE B
PRINT unionAB
PRINT intersectAB
PRINT diffAB
Output:
{1, 2, 3, 4, 5} {3} {1, 2}
Explanation:
- Union: Combines all unique elements from both sets.
- Intersection: Keeps only the elements found in both sets.
- Difference (A - B): Keeps elements found in A but not in B.
Example 4: Membership Testing
You can quickly test whether an item exists in a set.
SET vowels = {"a", "e", "i", "o", "u"}
IF "e" IN vowels THEN
PRINT "Yes"
ELSE
PRINT "No"
END IF
Output:
Yes
Question:
Why are sets considered more efficient for membership testing than lists?
Answer:
Because sets use hashing or similar structures internally to optimize lookup operations, making them typically faster (often constant time) compared to linear search in lists.
Best Practices
- Use sets when you want to enforce uniqueness automatically.
- Sets are ideal for comparing groups of items (e.g., finding common elements).
- Remember that sets are unordered — there's no guarantee about the position of elements.
Conclusion
Sets are a foundational data structure in programming, offering elegant solutions for problems involving uniqueness, membership, and mathematical group operations. Once understood, they become an indispensable tool in your problem-solving toolkit.