HashMap

Run Settings
LanguageJavaScript
Language Version
Run Command
class HashMap { constructor(size) { this.data = new Array(size); } set(key, value) { const hash = this._hash(key); if( !this.data[hash] ) { this.data[hash] = []; } this.data[hash].push([key, value]); } get(key) { const hash = this._hash(key); let bucket = this.data[hash]; let value; if( bucket ) { bucket.forEach( element => { if (element[0] == key) { value = element[1]; return; } }) } return value; } _hash(key) { let hash = 0; for(let i=0; i < key.length; i++) { hash = (hash + key.charCodeAt(i) * i) % this.data.length; } return hash; } } const hashMap = new HashMap(2); hashMap.set('woah', 950); hashMap.set('nope', 951); hashMap.set('neh', 952); hashMap.set('sing', 953); console.log(hashMap);
Editor Settings
Theme
Key bindings
Full width
Lines