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

  1. Create an empty collection.
  2. Traverse the array.
  3. Search for the current element in the collection.
  4. If it is not present, add it.
  5. 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

  1. Create an empty Set.
  2. Traverse the array.
  3. Insert every element into the Set.
  4. Duplicate elements are automatically ignored.
  5. 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

  1. Empty array: The Set remains empty.
  2. All elements unique: Every element is stored.
  3. All elements duplicated: Only one copy of each value remains.
  4. Single element: The Set contains one element.
  5. Negative values: Sets can store negative values as well.

Why This Problem is Important

Sets are commonly used when we need to:

  1. Remove duplicates
  2. Check element existence
  3. Store unique values
  4. Perform membership checks efficiently
  5. Solve hashing-based problems

Real-World Applications

Sets are useful in:

  1. Duplicate detection
  2. Database data cleaning
  3. Unique user identification
  4. Caching
  5. Membership checking

Common Mistakes

  1. Assuming a Set preserves the same ordering as every type of collection.
  2. Using a list when only uniqueness is required.
  3. Forgetting that duplicate values are automatically ignored.
  4. 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

  1. Introduction to Map
  2. Happy Number
  3. Contains Duplicate
  4. Intersection of Two Arrays
  5. 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.