Skip to content

Practice Design HashSet

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#705
LeetCode Problem

Design HashSet

Problem Statement

Implement MyHashSet without using a built-in hash-table library. add(key) makes the key present; adding an existing key has no additional effect. contains(key) reports presence. remove(key) makes the key absent and does nothing if it was already absent. Practice approach: Use a boolean direct-address array for the stated key range, or implement buckets and collision handling. Repeated insertions must not create duplicates that survive one removal. Test repeated insertion, absent-key removal, and both endpoint keys.

Source: Design HashSet on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["MyHashSet","add","add","contains","remove","contains","remove"]
[[],[6],[6],[6],[6],[6],[6]]
Output
[null,null,null,true,null,false,null]
Explanation

Adding 6 twice leaves one set entry; one removal makes contains(6) false.

Constraints

0 <= key <= 10^6
At most 10^4 total calls to add, remove, and contains.

Related LeetCode Problems

Practice more problems similar to Design HashSet.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.