Skip to content

Commit 9eb1b3c

Browse files
Create MyHashSet class with basic operations
Implemented a custom HashSet class with add, remove, and contains methods.
1 parent b0c2443 commit 9eb1b3c

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

Arrays/MyHashSet.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class MyHashSet { // Design a HashSet without a HashSet
2+
3+
List<Integer> set;
4+
5+
public MyHashSet() {
6+
set = new ArrayList<>();
7+
}
8+
9+
public void add(int key) {
10+
for(int i = 0; i<set.size(); i++){
11+
if(key == set.get(i)){
12+
return;
13+
}
14+
}
15+
16+
set.add(key);
17+
}
18+
19+
public void remove(int key) {
20+
21+
if(set.size() == 0){
22+
return;
23+
}
24+
25+
set.remove(Integer.valueOf(key));
26+
}
27+
28+
public boolean contains(int key) {
29+
for(int i = 0; i<set.size(); i++){
30+
if(key == set.get(i)){
31+
return true;
32+
}
33+
}
34+
35+
return false;
36+
}
37+
}
38+
39+
/**
40+
* Your MyHashSet object will be instantiated and called as such:
41+
* MyHashSet obj = new MyHashSet();
42+
* obj.add(key);
43+
* obj.remove(key);
44+
* boolean param_3 = obj.contains(key);
45+
*/

0 commit comments

Comments
 (0)