Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
C++ solution for design hash set
  • Loading branch information
nkawaller committed Nov 10, 2023
commit 9419d3fbc6262639ed6cd344d91cddbb6d21cb87
25 changes: 25 additions & 0 deletions cpp/0705-design-hash-set.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Time: O(n)
// Space: O(n)

class MyHashSet {
public:
void add(int key) {
if (!contains(key)) {
hashSet.push_back(key);
}
}

void remove(int key) {
auto k = find(hashSet.begin(), hashSet.end(), key);
if (k != hashSet.end()) {
hashSet.erase(k);
}
}

bool contains(int key) {
return (find(hashSet.begin(), hashSet.end(), key) != hashSet.end());
}

private:
vector<int> hashSet;
};