Last active
February 25, 2020 12:53
-
-
Save o-henry/ebe64a21afef2d69a3a30b0f66be7c41 to your computer and use it in GitHub Desktop.
Hash-Table
This file contains 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.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