Skip to content

Commit efd09ef

Browse files
Merge pull request codemistic#463 from Aman5989/patch-13
create design-hashmap.cpp
2 parents 71a7a4d + 62f1c9b commit efd09ef

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Hashtable/design-hashmap.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class MyHashMap {
2+
public:
3+
MyHashMap() {
4+
5+
}
6+
vector<pair<int,int>> bucket[100]; //bucket is an array whose each element is a vector of pair
7+
8+
void put(int key, int value) {
9+
int index = key%100;
10+
for(int i=0 ; i<bucket[index].size(); i++){
11+
if(bucket[index][i].first==key){
12+
bucket[index][i].second=value;
13+
return;
14+
}
15+
}
16+
bucket[index].push_back({key,value});
17+
}
18+
19+
int get(int key) {
20+
int index = key%100;
21+
for(int i=0 ; i<bucket[index].size(); i++){
22+
if(bucket[index][i].first==key)
23+
return bucket[index][i].second;
24+
}
25+
return -1;
26+
}
27+
28+
void remove(int key) {
29+
int index = key%100;
30+
for(int i=0 ; i<bucket[index].size(); i++){
31+
if(bucket[index][i].first==key){
32+
bucket[index].erase(bucket[index].begin() + i);
33+
return;
34+
}
35+
}
36+
}
37+
};

0 commit comments

Comments
 (0)