From 38b208957210a61e11bfa0ed35506e52e1aa1584 Mon Sep 17 00:00:00 2001 From: AmbrishRamachandiran Date: Thu, 5 Oct 2023 16:40:17 +0530 Subject: [PATCH 1/2] feat: HashTableSearch algorithm Signed-off-by: AmbrishRamachandiran --- Search/HashTableSearch.js | 43 +++++++++++++++++++++++++++++ Search/test/HashTableSearch.test.js | 30 ++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 Search/HashTableSearch.js create mode 100644 Search/test/HashTableSearch.test.js diff --git a/Search/HashTableSearch.js b/Search/HashTableSearch.js new file mode 100644 index 0000000000..601e5b0e69 --- /dev/null +++ b/Search/HashTableSearch.js @@ -0,0 +1,43 @@ +/* HashTable Search: https://en.wikipedia.org/wiki/Hash_table + * + * A hashtable, also known as a hash table or hash map, is a data structure that allows you to store and retrieve values using a key. + * It is an efficient way to implement a dictionary-like data structure where you can quickly access values based on their associated keys. + * Hashtable search involves using the key to locate the corresponding value in the hashtable. + */ + +class HashTable { + constructor() { + this.table = {}; + } + + // Function to hash the key + hash(key) { + let hash = 0; + for (let i = 0; i < key.length; i++) { + hash += key.charCodeAt(i); + } + return hash; + } + + // Function to insert a key-value pair into the hashtable + insert(key, value) { + const index = this.hash(key); + this.table[index] = value; + } + + // Function to search for a value by key + search(key) { + const index = this.hash(key); + return this.table[index]; + } + + // Function to remove a key-value pair from the hashtable + remove(key) { + const index = this.hash(key); + if (this.table[index]) { + delete this.table[index]; + } + } + } + + module.exports = HashTable; \ No newline at end of file diff --git a/Search/test/HashTableSearch.test.js b/Search/test/HashTableSearch.test.js new file mode 100644 index 0000000000..bf41a3be40 --- /dev/null +++ b/Search/test/HashTableSearch.test.js @@ -0,0 +1,30 @@ +const HashTable = require('./HashTable'); + +test('Inserting and searching for values in the hashtable', () => { + const hashtable = new HashTable(); + + // Insert key-value pairs + hashtable.insert("name", "Ambrish"); + hashtable.insert("age", 26); + hashtable.insert("city", "India"); + + // Search for values and expect them to be correct + expect(hashtable.search("name")).toBe("Ambrish"); + expect(hashtable.search("age")).toBe(26); + expect(hashtable.search("city")).toBe("India"); + + // Search for a non-existent key and expect it to be undefined + expect(hashtable.search("country")).toBeUndefined(); +}); + +test('Removing values from the hashtable', () => { + const hashtable = new HashTable(); + + // Insert a key-value pair + hashtable.insert("name", "Ambrish"); + + // Remove the key-value pair and expect it to be undefined + hashtable.remove("name"); + expect(hashtable.search("name")).toBeUndefined(); +}); + From e498f5d33abc9fc2eab4bdbb013bd43821315f95 Mon Sep 17 00:00:00 2001 From: AmbrishRamachandiran Date: Thu, 5 Oct 2023 16:53:34 +0530 Subject: [PATCH 2/2] feat: HashTableSearch algorithm Signed-off-by: AmbrishRamachandiran --- Search/test/HashTableSearch.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Search/test/HashTableSearch.test.js b/Search/test/HashTableSearch.test.js index bf41a3be40..ed328331f2 100644 --- a/Search/test/HashTableSearch.test.js +++ b/Search/test/HashTableSearch.test.js @@ -1,4 +1,4 @@ -const HashTable = require('./HashTable'); +import HashTable from "../HashTableSearch"; test('Inserting and searching for values in the hashtable', () => { const hashtable = new HashTable();