Skip to main content
Back to problems
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): Insert key into the set if it is not already present.
  • remove(key): Remove key from the set if it exists.
  • contains(key): Return true if key exists in the set, otherwise false.

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 O(1)O(1) 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
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.