-
Notifications
You must be signed in to change notification settings - Fork 277
/
DesignHashSet.java
205 lines (162 loc) · 4.37 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
class MyHashSet {
boolean[] set = null;
/** Initialize your data structure here. */
public MyHashSet() {
set = new boolean[1000001];
}
public void add(int key) {
set[key] = true;
}
public void remove(int key) {
set[key] = false;
}
/** Returns true if this set contains the specified element */
public boolean contains(int key) {
return set[key];
}
}
class MyHashSet {
// Hashset => 4(1), 10(2), 16(1) , 17(3)
/*
Hash
1 -> 4 -> 16 ->.... Time complexity => o(n)
2 -> 10 -> ...
3 -> 17 -> ...
*/
/** Initialize your data structure here. */
private int capacity = 0;
private List<Integer>[] set = null;
public MyHashSet() {
capacity = 1500;
set = new List[capacity];
}
private int getKeyHash(int key){
return key % capacity;
}
public void add(int key) {
int hashIndex = getKeyHash(key);
if(set[hashIndex] == null){
set[hashIndex] = new LinkedList<>();
}
if(set[hashIndex].indexOf(key) == -1){
set[hashIndex].add(key);
}
}
public void remove(int key) {
if(contains(key)) {
int hashIndex = getKeyHash(key);
// removing the key from the set
set[hashIndex].remove(set[hashIndex].indexOf(key));
}
}
/** Returns true if this set contains the specified element */
public boolean contains(int key) {
int hashIndex = getKeyHash(key);
if(set[hashIndex] == null || set[hashIndex].indexOf(key) == -1){
return false;
} else {
return true;
}
}
}
/**
* 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);
*/
class MyHashSet {
// Hashset => 4(1), 10(2), 16(1) , 17(3)
/*
Hash
1 -> 4 -> 16 ->.... Time complexity => o(n)
2 -> 10 -> ...
3 -> 17 -> ...
*/
/** Initialize your data structure here. */
private int capacity = 0;
private List<Integer>[] set = null;
public MyHashSet() {
capacity = 1500;
set = new List[capacity];
}
private int getKeyHash(int key){
return key % capacity;
}
public void add(int key) {
int hashIndex = getKeyHash(key);
if(set[hashIndex] == null){
set[hashIndex] = new LinkedList<>();
}
if(set[hashIndex].indexOf(key) == -1){
set[hashIndex].add(key);
}
}
public void remove(int key) {
if(contains(key)) {
int hashIndex = getKeyHash(key);
// removing the key from the set
set[hashIndex].remove(set[hashIndex].indexOf(key));
}
}
/** Returns true if this set contains the specified element */
public boolean contains(int key) {
int hashIndex = getKeyHash(key);
if(set[hashIndex] == null || set[hashIndex].indexOf(key) == -1){
return false;
} else {
return true;
}
}
}
/**
* 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);
*/
// @saorav21994
// TC : O(1)
// SC : O(n)
// This can also be done in O(log10n) - Space + Time using Trie. At max 7 loop in each add/remove call.
// This can also be done using buckets + linkedList approach.
class MyHashSet {
boolean [] hash;
public MyHashSet() {
hash = new boolean[1000010];
}
public void add(int key) {
hash[key] = true;
}
public void remove(int key) {
hash[key] = false;
}
public boolean contains(int key) {
return hash[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);
*/
// Author: Shobhit Behl (LC: shobhitbruh)
class MyHashSet {
HashSet<Integer> hs;
public MyHashSet() {
hs=new HashSet<>();
}
public void add(int key) {
hs.add(key);
}
public void remove(int key) {
hs.remove(key);
}
public boolean contains(int key) {
return hs.contains(key);
}
}