forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesign HashSet.java
More file actions
38 lines (33 loc) · 890 Bytes
/
Design HashSet.java
File metadata and controls
38 lines (33 loc) · 890 Bytes
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
32
33
34
35
36
37
38
class MyHashSet {
/** Initialize your data structure here. */
List<Integer> list;
public MyHashSet() {
list = new ArrayList<>();
}
public void add(int key) {
if (!list.contains(key)) {
list.add(key);
}
}
public void remove(int key) {
Iterator<Integer> it = list.iterator();
while(it.hasNext()) {
int val = it.next();
if (val == key) {
it.remove();
return;
}
}
}
/** Returns true if this set did not already contain the specified element */
public boolean contains(int key) {
return list.contains(key);
}
}
/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet obj = new MyHashSet();
* obj.add(key);
* obj.remove(key);
* boolean param_3 = obj.contains(key);
*/