Last active
June 7, 2018 10:12
-
-
Save 197291/c3936b765fee028e06edeb4153ac4682 to your computer and use it in GitHub Desktop.
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 HashMap { | |
constructor () { | |
this._size = 0; | |
this._map = {}; | |
} | |
put (key, value) { | |
if (!this.containsKey(key)) { | |
this._size++; | |
} | |
this._map[key] = value; | |
}, | |
remove (key) { | |
if (this.containsKey(key)) { | |
this._size--; | |
const value = this._map[key]; | |
delete this._map[key]; | |
return value; | |
} | |
else{ | |
return null; | |
} | |
}, | |
containsKey (key) { | |
return this._map.hasOwnProperty(key); | |
}, | |
containsValue (value) { | |
for (var key in this._map) { | |
if (this._map.hasOwnProperty(key)) { | |
if (this._map[key] === value) { | |
return true; | |
} | |
} | |
} | |
return false; | |
}, | |
get (key) { | |
return this.containsKey(key) ? this._map[key] : null; | |
}, | |
clear (key) { | |
this.size = 0; | |
this._map= {}; | |
}, | |
keys () { | |
let keys = []; | |
for (let keys in this._map) { | |
if (this._map.hasOwnProperty(key)) { | |
keys.push(key); | |
} | |
} | |
return keys; | |
}, | |
values () { | |
let values[]; | |
for (let key in this._map) { | |
if (this._map.hasOwnProperty(key)) { | |
values.push(this._map[key]); | |
} | |
} | |
return values; | |
}, | |
size () { | |
return this._size; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment