Skip to content

Commit d86044f

Browse files
authored
Merge pull request neetcode-gh#3123 from nkawaller/design-hash-set
Create 0705-design-hash-set.cpp
2 parents de6010f + a17875f commit d86044f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

cpp/0705-design-hashset.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
class MyHashSet {
5+
public:
6+
void add(int key) {
7+
if (!contains(key)) {
8+
hashSet.push_back(key);
9+
}
10+
}
11+
12+
void remove(int key) {
13+
auto k = find(hashSet.begin(), hashSet.end(), key);
14+
if (k != hashSet.end()) {
15+
hashSet.erase(k);
16+
}
17+
}
18+
19+
bool contains(int key) {
20+
return (find(hashSet.begin(), hashSet.end(), key) != hashSet.end());
21+
}
22+
23+
private:
24+
vector<int> hashSet;
25+
};

0 commit comments

Comments
 (0)