Code-Memo

HashSet

A HashSet is a data structure that stores unique elements only and offers O(1) average time complexity for insertion, deletion, and lookup. It’s backed by a hash table just like a Hashmap, but only stores keys, no values.

It’s implemented using the built-in set type.

Initialization

# Empty set
s = set()

# With values
s = {1, 2, 3}

Basic Operations

1. Insert (Add Element)

s.add(4)        # Adds 4
s.add(2)        # Already exists, no effect

2. Remove Element

s.remove(2)     # Removes 2 (raises KeyError if not found)
s.discard(5)    # Safe remove (no error if 5 not present)

3. Check Existence

if 3 in s:
    print("Exists")

4. Traversal

for item in s:
    print(item)

5. Size

len(s)  # Number of unique elements

6. Clear Set

s.clear()

Set Operations

Useful for solving problems involving duplicates, intersections, or differences.

1. Union (OR)

a = {1, 2, 3}
b = {3, 4, 5}
a | b      # {1, 2, 3, 4, 5}

2. Intersection (AND)

a & b      # {3}

3. Difference (A - B)

a - b      # {1, 2}

4. Symmetric Difference (A XOR B)

a ^ b      # {1, 2, 4, 5}

Use Cases