Last active
January 31, 2021 14:07
-
-
Save naveenrawat51/dc8f7b0cf6bd1355198157bcbcde1498 to your computer and use it in GitHub Desktop.
Hash Table implementation in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HashTable { | |
constructor(size) { | |
this.data = new Array(size); | |
} | |
// hash method | |
hashKey(key) { | |
let hash = 0; | |
for (let i = 0; i < key.length; i++) { | |
hash = (hash + key.charCodeAt(i) * i) % this.data.length; | |
} | |
return hash; | |
} | |
// set a key value in hash table | |
set(key, value) { | |
const address = this.hashKey(key); | |
if (!this.data[address]) { | |
this.data[address] = []; | |
} | |
this.data[address].push([key, value]); | |
return this.data; | |
} | |
// get the item from hash table | |
get(key) { | |
const address = this.hashKey(key); | |
const getArray = this.data[address]; | |
if (getArray && getArray.length > 1) { | |
for (let item of getArray) { | |
if (item[0] === key) { | |
return item; | |
} | |
} | |
} | |
return (getArray && getArray[0]) || 'Record not found'; | |
} | |
} | |
const hash1 = new HashTable(6); | |
console.log(hash1.set('naveen', 32)); | |
console.log(hash1.set('sangeeta', 24)); | |
console.log(hash1.set('kamal', 24)); | |
console.log(hash1.set('arya', 24)); | |
console.log(hash1.set('sanju', 24)); | |
console.log(hash1.get('naveen')); | |
console.log(hash1.get('sangeeta')); | |
console.log(hash1.get('sanju')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment