Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

4 Commits

Repository files navigation

Hash Table Data Structure in JavaScript 🚀

A simple implementation of the Hash Table data structure in JavaScript. This repository demonstrates how to create a hash table class with essential methods and explains its functionality with practical examples.


What is a Hash Table?

A Hash Table (or Hash Map) is a data structure that stores key-value pairs. It uses a hash function to compute an index (hash) into an array of buckets or slots, from which the desired value can be found. Hash tables provide fast access to data and are widely used for implementing associative arrays or database indexing.


Features

  • Insert: Add a key-value pair to the hash table.
  • Delete: Remove a key-value pair from the hash table.
  • Search: Find a value by its key.
  • Size: Get the number of key-value pairs in the table.

Code Implementation

Here’s the JavaScript implementation of the hash table:

classHashTable{constructor(size=50){this.table=newArray(size);// Initialize the hash table with a given size}// Hash function to calculate the indexhash(key){lethash=0;for(leti=0;i<key.length;i++){hash=(hash<<5)+hash+key.charCodeAt(i);// Hashing algorithm}returnhash%this.table.length;// Return the index within table size}// Insert a key-value pair into the hash tableinsert(key,value){constindex=this.hash(key);if(!this.table[index]){this.table[index]=[];}this.table[index].push([key,value]);// Handle collisions by chaining}// Delete a key-value pair from the hash tabledelete(key){constindex=this.hash(key);if(this.table[index]){this.table[index]=this.table[index].filter(([k,v])=>k!==key);}}// Search for a value by its keysearch(key){constindex=this.hash(key);if(this.table[index]){for(let[k,v]ofthis.table[index]){if(k===key){returnv;}}}returnnull;// Return null if key is not found}// Get the size of the hash tablesize(){letcount=0;for(letbucketofthis.table){if(bucket){count+=bucket.length;}}returncount;}}

Example Usage

// Initialize the hash tableconsthashTable=newHashTable();// Insert key-value pairshashTable.insert("name","John");hashTable.insert("age",30);hashTable.insert("city","New York");// Search for a value by its keyconsole.log(hashTable.search("name"));// Output: Johnconsole.log(hashTable.search("age"));// Output: 30// Delete a key-value pairhashTable.delete("age");console.log(hashTable.search("age"));// Output: null// Get the size of the hash tableconsole.log(hashTable.size());// Output: 2

Real-World Applications

  1. Database Indexing: Used in databases to quickly find records.
  2. Caching: Storing computed results for faster access.
  3. Implementing Sets: Storing unique elements without duplicates.
  4. Associative Arrays: Used in languages like JavaScript and Python to map keys to values.

TikTok Tutorial 🎥

Want to see a quick tutorial on how to build this? Check out this TikTok video:


How to Run the Code

  1. Clone the repository:
    git clone https://github.com/your-username/hash-table-data-structure.git
    cd hash-table-data-structure
  2. Open the file hash-table.js in your favorite code editor.
  3. Run the file using Node.js:
    node hash-table.js

Contributing

Contributions are welcome! If you have suggestions or want to add new features, feel free to create a pull request.


License

This project is licensed under the MIT License.


Connect with Me:

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages