Skip to content

Instantly share code, notes, and snippets.

@o-henry
Last active February 25, 2020 12:53
Show Gist options
  • Save o-henry/ebe64a21afef2d69a3a30b0f66be7c41 to your computer and use it in GitHub Desktop.
Save o-henry/ebe64a21afef2d69a3a30b0f66be7c41 to your computer and use it in GitHub Desktop.
Hash-Table
class HashTable{
constructor(size){
this.buckets = new Array(size)
this.size = size
}
hashFunc(key) {
return key.toString().length % this.size;
}
set(key, value){
// 인덱스는 value가 되네요
let idx = this.hashFunc(key);
if(!this.buckets[idx]){
this.buckets[idx] = [];
}
this.buckets[idx].push([key, value])
}
get(key){
let index = this.hash(key);
if(!this.buckets[index]) return null
for(let bucket of this.buckets[index]){
// key
if(bucket [0] === key){
//value
return bucket[1]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment