Applied AI Engineer (LLM) coding learning path Level 1 This level · 6 of 9

Design HashSet

Easy Design Array Hash map

Problem

Design a HashSet without using the language's built-in set. Support add, remove and contains for non-negative integers.

Original: LeetCode 705: Design HashSet.

Examples

Example 1

Input`MyHashSet: add(1), add(2), contains(1), contains(3), add(2), contains(2), remove(2), contains(2)`
Output`[null, null, true, false, null, true, null, false]`

Constraints

  • 0 <= key <= 10^6
  • At most 10^4 calls will be made to add, remove, and contains.

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (2)
  1. LeetCode 705: Design HashSetleetcode.com
  2. Java SE 17 API: ArrayList.removedocs.oracle.com
Previous
Next