Introduction
A Set is a data structure used to store unique elements.
Unlike an array, a Set does not allow duplicate values. It is useful when we need to quickly check whether an element exists or remove duplicate values.
This concept helps in understanding:
- Unique elements
- Hashing
- Fast searching
- Insertion and deletion
- Duplicate removal
Problem Statement
Given an array of integers, create a Set containing only the unique elements.
For example, if the array contains [1, 2, 2, 3, 4, 4], the Set should contain [1, 2, 3, 4].
Example
Input:nums = [1, 2, 2, 3, 4, 4]
Output:
[1, 2, 3, 4]
Explanation:
Duplicate elements 2 and 4 are stored only once in the Set.
Approach 1: Brute Force
Explanation
Without using a built-in Set, we can maintain a separate collection of unique elements.
For every element, search the collection to determine whether it already exists.
If it does not exist, add it.
Steps
- Create an empty collection.
- Traverse the array.
- Search for the current element in the collection.
- If it is not present, add it.
- Continue until all elements are processed.
Dry Run
Input:nums = [1, 2, 2, 3]
Start:
unique = []
1 → not present → add
unique = [1]
2 → not present → add
unique = [1, 2]
2 → already present → skip
3 → not present → add
unique = [1, 2, 3]
Final Result:
[1, 2, 3]
Brute Force Code
Complexity Analysis
Time Complexity: O(n²)
For every element, we may search through the unique elements.
Space Complexity: O(n)
An additional collection is used to store unique elements.
Approach 2: Optimized Solution
Explanation
The optimized approach uses a Set.
A Set automatically stores each element only once.
When an element is inserted:
- If it is new, it is added.
- If it already exists, the duplicate is ignored.
This allows efficient insertion and searching.
Steps
- Create an empty Set.
- Traverse the array.
- Insert every element into the Set.
- Duplicate elements are automatically ignored.
- Return the Set.
Dry Run
Input:nums = [1, 2, 2, 3, 4, 4]
Start:
Set = {}
1 → add → {1}
2 → add → {1, 2}
2 → already exists → {1, 2}
3 → add → {1, 2, 3}
4 → add → {1, 2, 3, 4}
4 → already exists → {1, 2, 3, 4}
Final Result:
{1, 2, 3, 4}
Optimized Code
Complexity Analysis
Time Complexity: O(n) average
Each element is inserted into the Set in average constant time.
Space Complexity: O(n)
The Set can store up to n unique elements.
Common Set Operations
A Set commonly supports the following operations:
- Insert: Add an element.
- Search: Check whether an element exists.
- Delete: Remove an element.
- Size: Find the number of unique elements.
Edge Cases
- Empty array: The Set remains empty.
- All elements unique: Every element is stored.
- All elements duplicated: Only one copy of each value remains.
- Single element: The Set contains one element.
- Negative values: Sets can store negative values as well.
Why This Problem is Important
Sets are commonly used when we need to:
- Remove duplicates
- Check element existence
- Store unique values
- Perform membership checks efficiently
- Solve hashing-based problems
Real-World Applications
Sets are useful in:
- Duplicate detection
- Database data cleaning
- Unique user identification
- Caching
- Membership checking
Common Mistakes
- Assuming a Set preserves the same ordering as every type of collection.
- Using a list when only uniqueness is required.
- Forgetting that duplicate values are automatically ignored.
- Confusing a Set with a Map.
Interview Tips
When a problem asks for unique elements or requires frequent existence checks, think about using a Set.
The key advantage is that hashing-based Sets provide average O(1) insertion and lookup.
Related Questions
- Introduction to Map
- Happy Number
- Contains Duplicate
- Intersection of Two Arrays
- Longest Consecutive Sequence
Final Takeaway
A Set is a useful data structure for storing unique elements and performing fast membership operations.
The brute force approach manually searches for duplicates, while the optimized approach uses a Set to handle uniqueness efficiently.