Skip to content

Implementation of the HashMap Project from The Odin Project — building a custom hash map in JavaScript with hashing, collision handling, resizing, and key-value operations.

Notifications You must be signed in to change notification settings

devxsameer/odin-hashmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🗂️ HashMap & HashSet Implementation (JavaScript)

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.


📚 Overview

🔹 HashMap

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

🔹 HashSet

A HashSet stores unique values only (no duplicates).
It is built with the same hashing principles as HashMap, but only tracks keys.


⚡ Features

HashMap

  • set(key, value) → insert or update
  • get(key) → retrieve value
  • has(key) → check existence
  • remove(key) → delete key–value
  • length() → number of entries
  • clear() → reset map
  • keys() → return array of keys
  • values() → return array of values
  • entries() → return [key, value] pairs
  • Auto resizing when load factor exceeded

HashSet

  • add(value) → insert unique value
  • has(value) → check existence
  • remove(value) → delete value
  • length() → number of values
  • clear() → reset set
  • values() → return all values
  • Auto resizing when load factor exceeded

🚀 Getting Started

  1. Clone the repository

    git clone https://github.com/devxsameer/odin-hashmap
    cd odin-hashmap
  2. Run the example

    node index.js

🖥️ Example Output

=== 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

🛠️ Technologies

  • JavaScript (ES6+)
  • Node.js (for running demo)

🎯 Learning Outcomes

  • Understand hashing functions and collisions
  • Implement resizing and rehashing
  • See how HashMap and HashSet are built internally
  • Practice JavaScript classes and modules

📝 License

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.

About

Implementation of the HashMap Project from The Odin Project — building a custom hash map in JavaScript with hashing, collision handling, resizing, and key-value operations.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published