-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesignHashSet.java
39 lines (32 loc) · 941 Bytes
/
DesignHashSet.java
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
39
import java.util.*;
import java.util.lang.*;
import java.io.*;
class MyHashSet {
/** Initialize your data structure here. */
List<Integer>list=new ArrayList<>();
public MyHashSet() {
list=new ArrayList<>();
}
public void add(int key) {
list.add(key);
}
public void remove(int key) {
Iterator<Integer>it=list.iterator();
while(it.hasNext()){ int value=it.next();
if(value==key) {
it.remove();
}}
}
/** Returns true if this set contains the specified element */
public boolean contains(int key) {
if(list.contains(key))return true;
else return false;
}
}
/**
* 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);
*/