Skip to content

Instantly share code, notes, and snippets.

@kevinmmartins
Created June 13, 2022 18:47
Show Gist options
  • Save kevinmmartins/daa1c793bf27fc3cebe089cf35e94efe to your computer and use it in GitHub Desktop.
Save kevinmmartins/daa1c793bf27fc3cebe089cf35e94efe to your computer and use it in GitHub Desktop.
Simple map class
// Implement a key-value store with get(key), put(key, value), delete(key) operations.
class CustomMap {
constructor() {
this._keys = [];
this._values = [];
};
put(key,value) {
this._keys.push(key)
this._values.push(value)
}
_getPositionByKey (key) {
const position = this._keys.indexOf(key)
return position === -1 ? null : position
}
get(key) {
const position = this._getPositionByKey(key)
return this._values[position]
}
delete(key) {
const position = this._getPositionByKey(key)
delete this._keys[position]
delete this._values[position]
}
}
const map = new CustomMap()
map.put('kevin', 'uber')
map.put('nevil', 'uber2')
map.put('number', 124)
console.log(map.get('kevin'))
console.log(map.get('Null Test'))
console.log(map.get('nevil'))
console.log(map.get('number'))
map.delete("kevin")
console.log(map.get('nevil'))
console.log(map.get('kevin'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment