githubEdit

705. Design HashSet

🟩 Easy

Design a HashSet without using any built-in hash table libraries.

Implement MyHashSet class:

  • void add(key) Inserts the value key into the HashSet.

  • bool contains(key) Returns whether the value key exists in the HashSet or not.

  • void remove(key) Removes the value key in the HashSet. If key does not exist in the HashSet, do nothing.

Example 1

Input: ["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"] [[], [1], [2], [1], [3], [2], [2], [2], [2]] Output: [null, null, null, true, false, null, true, null, false] Explanation: MyHashSet myHashSet = new MyHashSet(); myHashSet.add(1); // set = [1] myHashSet.add(2); // set = [1, 2] myHashSet.contains(1); // return True myHashSet.contains(3); // return False, (not found) myHashSet.add(2); // set = [1, 2] myHashSet.contains(2); // return True myHashSet.remove(2); // set = [1] myHashSet.contains(2); // return False, (already removed)

Constraints

  • 0 <= key <= 10^6

  • At most 10^4 calls will be made to add, remove, and contains.

Solution

My Solution (Chaining with Linked Lists)

type Node struct {
    Value int
    Next  *Node
}

type MyHashSet struct {
    buckets []*Node
    count   int
}

func Constructor() MyHashSet {
    buckets := make([]*Node, 4)
    return MyHashSet{
        buckets: buckets,
        count:   0,
    }
}

func (h *MyHashSet) index(key int) int {
    return key % len(h.buckets)
}

func (h *MyHashSet) Add(key int) {
    idx := h.index(key)

    curr := h.buckets[idx]

    if curr == nil {
        h.buckets[idx] = &Node{Value: key}
        h.count++
        return
    }

    for curr.Next != nil {
        if curr.Value == key {
            return
        }

        curr = curr.Next
    }

    if curr.Value != key {
        curr.Next = &Node{Value: key}
        h.count++
    }

    if float64(h.count)/float64(len(h.buckets)) > 0.75 {
        h.resize()
    }
}

func (h *MyHashSet) resize() {
    oldBuckets := h.buckets
    h.buckets = make([]*Node, len(h.buckets)*2)
    h.count = 0

    for _, key := range oldBuckets {
        for key != nil {
            h.Add(key.Value)
            key = key.Next
        }
    }
}

func (h *MyHashSet) Remove(key int) {
    idx := key % len(h.buckets)

    curr := h.buckets[idx]

    if curr != nil && curr.Value == key {
        h.buckets[idx] = h.buckets[idx].Next
        h.count--
        return
    }

    for curr != nil && curr.Next != nil {
        if curr.Next.Value == key {
            curr.Next = curr.Next.Next
            h.count--
            return
        }

        curr = curr.Next
    }
}

func (h *MyHashSet) Contains(key int) bool {
    idx := key % len(h.buckets)

    curr := h.buckets[idx]

    for curr != nil {
        if curr.Value == key {
            return true
        }

        curr = curr.Next
    }

    return false
}

Optimal Solution 1 (Open Addressing with Linear Probing)

Optimal Solution 2 (Bit Vector for Small Keys)

Approach Analysis

This problem showcases different hash set implementation strategies:

  1. Chaining with Linked Lists (Your Solution):

    • Separate chaining for collisions

    • Dynamic resizing

    • Linked list traversal

    • Good for high load factors

  2. Open Addressing:

    • Linear probing

    • In-place collision resolution

    • Efficient cache usage

    • Better for low load factors

  3. Bit Vector:

    • Direct mapping

    • Bit-level operations

    • No collisions

    • Perfect for integers

Visualization of Approaches

Chaining Process (Your Solution)

Open Addressing Process

Bit Vector Process

Complexity Analysis

Chaining Solution (Your Solution)

  • Time:

    • Add: O(1) average, O(n) worst

    • Remove: O(1) average, O(n) worst

    • Contains: O(1) average, O(n) worst

  • Space: O(n)

    • Linked list nodes

    • Dynamic resizing

    • Load factor control

Open Addressing Solution

  • Time:

    • Add: O(1) average, O(n) worst

    • Remove: O(1) average, O(n) worst

    • Contains: O(1) average, O(n) worst

  • Space: O(n)

    • Contiguous array

    • Better cache locality

    • Load factor < 0.75

Bit Vector Solution

  • Time:

    • Add: O(1)

    • Remove: O(1)

    • Contains: O(1)

  • Space: O(M)

    • M = max key value

    • Very space efficient

    • Fixed size array

Why Solutions Work

  1. Chaining Logic:

    • Distributes collisions

    • Maintains insertion order

    • Easy to implement

    • Flexible growth

  2. Open Addressing:

    • Cache-friendly

    • No extra pointers

    • Simple probing

    • Good locality

  3. Bit Vector:

    • Direct mapping

    • Bit-level operations

    • No collisions

    • Perfect for integers

When to Use

  1. Chaining When:

    • High load factor

    • Memory not critical

    • Order matters

    • Many collisions

  2. Open Addressing When:

    • Cache performance critical

    • Memory contiguous

    • Low load factor

    • Few collisions

  3. Bit Vector When:

    • Small key range

    • Memory critical

    • Integer keys only

    • Fast operations needed

Common Patterns & Applications

  1. Related Problems:

    • Design HashMap

    • LRU Cache

    • Insert Delete GetRandom O(1)

    • Find Duplicate

  2. Key Techniques:

    • Hash functions

    • Collision handling

    • Dynamic resizing

    • Memory management

Interview Tips

  1. Solution Highlights:

    • Collision handling

    • Load factor management

    • Space efficiency

    • Time complexity

  2. Common Pitfalls:

    • Poor hash function

    • Missing edge cases

    • Memory leaks

    • Infinite loops

  3. Testing Strategy:

    • Empty set

    • Duplicate keys

    • Collisions

    • Deletions

    • Resizing

  4. Follow-up Questions:

    • Thread safety?

    • Custom objects?

    • Persistence?

    • Distribution?

result

Leetcode: linkarrow-up-right

Last updated