Skip to content

Instantly share code, notes, and snippets.

@fazeelanizam13
Created May 21, 2022 15:00
Show Gist options
  • Save fazeelanizam13/07b2fcec44f41c2256d5dc2f143e66b0 to your computer and use it in GitHub Desktop.
Save fazeelanizam13/07b2fcec44f41c2256d5dc2f143e66b0 to your computer and use it in GitHub Desktop.
Implementation of the hash table data structure in JavaScript.
const hashFunction = (key, tableSize) => {
// index we are going to return
let index = 0
// get list of characters in key string as an array
let numChars = String(key)
// assign numChars size to index to start
index = numChars.length
// loop through numChars and keep multiplying index by unicode of current character
for (let i = 0; i < numChars.length; i++) {
index *= numChars.charCodeAt(i)
}
// use mod to return a number between 0 and tableSize as index
return index % tableSize
}
class HashTable {
constructor (size) {
this.table = new Array(size)
this.size = this.table.length
}
has = key => {
let index = hashFunction(key, this.size)
// if value doesn't exist return false
if (!this.table[index]) return false
// else find the [key, value] pair with given key and return true if pair with key exists
else {
if (this.table[index].find(item => item[0] === key)) return true
else return false
}
}
get = key => {
let index = hashFunction(key, this.size)
// if key doesn't exist return null
if (!this.table[index]) return null
// else find the [key, value] pair with given key and return second element i.e. value part of [key, value] if exists
// return null if doesn't
else {
if (this.table[index].find(item => item[0] === key)) {
return this.table[index].find(item => item[0] === key)[1]
}
else return null
}
}
set = ([key, value]) => {
let index = hashFunction(key, this.size)
// if key doesn't exist at the index already, set an array with [key, value]
if (!this.table[index]) this.table[index] = [[key, value]]
// else push [key, value] to existing array
else this.table[index].push([key, value])
}
remove = ([key, value]) => {
let index = hashFunction(key, this.size)
// if key doesn't exist at the index, return null
if (!this.table[index]) this.table[index] = [[key, value]]
// else find the [key, value] pair with given key and value and remove it from index
// return null if doesn't
else {
if (this.table[index].find(item => item[0] === key && item[1] === value)) {
// [key, value] pair
let item = this.table[index].find(item => item[0] === key && item[1] === value)
// index of [key, value] pair
let subIndex = this.table[index].indexOf(item)
// remove item at subIndex
this.table[index].splice(subIndex, 1)
return [key, value]
}
else return null
}
}
}
// tests
const hashTable = new HashTable(40)
hashTable.set(['Abigail', '26'])
console.log(hashTable)
hashTable.set(['Tallie', '25'])
console.log(hashTable)
hashTable.set(['Jean', '28'])
console.log(hashTable)
console.log(hashTable.has('Abigail'))
console.log(hashTable.get('Abigail'))
console.log(hashTable.has('Lydia'))
console.log(hashTable.get('Lydia'))
console.log(hashTable.remove(['Abigail', '26']))
console.log(hashTable.has('Abigail'))
console.log(hashTable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment