-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashmap.js
31 lines (27 loc) · 940 Bytes
/
Hashmap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
Design a hashmap without using any built-in libraries. You should include the following functions:
put(key, value): Insert a (key, value) pair into the hashmap. If the value already exists, update the value.
get(key): Returns the value to which key is mapped, or -1 if this map contains nothing for key.
remove(key): Remove the mapping for the value in key if it exists.
*/
class Hashmap{
constructor(dict = {}){
this.dict = dict;
}
put(key, value){
this.dict[key] = value;
}
get(key){
return key in this.dict ? this.dict[key]:-1;
}
}
let map = new Hashmap();
map.put("year", 2021);
map.put("month", "january");
map.put("number", 123);
console.log(map.get("number"));//123
map.put("number", 21); //update existing key
console.log(map.get("number"));//21
console.log(map.get("year")); //2021
console.log(map.get("month")); //january
console.log(map.get(1)); //-1