Back to problems Sign in to unlock
Leetcode
Medium
Sets
Hash Maps
Design
Design Hashset
Design a data structure that supports insert, remove, and membership checks for integers in average constant time.
Acceptance 0%
Problem Statement
Problem
Design a hash set from scratch for integer keys.
Implement a class that supports the following operations:
add(key): Insertkeyinto the set if it is not already present.remove(key): Removekeyfrom the set if it exists.contains(key): Returntrueifkeyexists in the set, otherwisefalse.
Your implementation should behave like a set: duplicate inserts should not create duplicate entries, and removing a missing key should do nothing.
Goal
Provide an interface with average time per operation.
Input Format
- A sequence of method calls on a hash set instance.
- Each operation uses one integer key.
- Keys may be positive, negative, or zero.
Output Format
- For each membership query, return whether the key is present.
- Insert and remove operations do not return a value unless specified by the interface.
Constraints
- Support many operations efficiently.
- Average time per operation should be constant.
- Keys are integers and may repeat across operations.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.