Implementation of the HashMap and HashSet data structures from scratch in JavaScript, as part of The Odin Project.
This project focuses on understanding how hashing works under the hood — buckets, collisions, resizing, and ensuring efficient data storage and retrieval.
A HashMap
stores key–value pairs with constant-time average complexity for:
- Insertion (
set
) - Lookup (
get
,has
) - Deletion (
remove
)
It uses:
- Buckets (arrays) for collision handling
- Resizing (rehashing) when the load factor exceeds
0.75
- Hashing function for string keys
A HashSet
stores unique values only (no duplicates).
It is built with the same hashing principles as HashMap
, but only tracks keys.
set(key, value)
→ insert or updateget(key)
→ retrieve valuehas(key)
→ check existenceremove(key)
→ delete key–valuelength()
→ number of entriesclear()
→ reset mapkeys()
→ return array of keysvalues()
→ return array of valuesentries()
→ return[key, value]
pairs- Auto resizing when load factor exceeded
add(value)
→ insert unique valuehas(value)
→ check existenceremove(value)
→ delete valuelength()
→ number of valuesclear()
→ reset setvalues()
→ return all values- Auto resizing when load factor exceeded
-
Clone the repository
git clone https://github.com/devxsameer/odin-hashmap cd odin-hashmap
-
Run the example
node index.js
=== HashMap Demo ===
Get name: Sameer
Has city? true
Map length: 3
After removing age, length: 2
Keys: [ 'name', 'city' ]
Values: [ 'Sameer', 'Moradabad' ]
Entries: [ [ 'name', 'Sameer' ], [ 'city', 'Moradabad' ] ]
After clear, length: 0
=== HashSet Demo ===
Has banana? true
Set length: 3
After removing cherry, length: 2
Values in set: [ 'banana', 'apple' ]
After clear, length: 0
- JavaScript (ES6+)
- Node.js (for running demo)
- Understand hashing functions and collisions
- Implement resizing and rehashing
- See how HashMap and HashSet are built internally
- Practice JavaScript classes and modules
This project is for learning purposes as part of The Odin Project Curriculum. Feel free to fork, play around, and improve!
Built with ❤️ by Sameer Ali
.