@@ -164,23 +164,29 @@ class HashTable {
164
164
return null ;
165
165
}
166
166
167
- // get all the values for the key to return
168
- // eslint-disable-next-line no-underscore-dangle
169
- const vals = this . _values ( index , key ) ;
170
- const nodes = [ ] ;
167
+ if ( head . key === key ) {
168
+ let node = head ;
169
+ this . bucket [ index ] = head . next ;
170
+ const val = { key, value : node . value } ;
171
+ node = null ;
172
+ this . size -= 1 ;
173
+ return val ;
174
+ }
175
+
176
+ let previous = null ;
177
+
171
178
while ( head !== null ) {
172
- if ( head . key !== key ) {
173
- nodes . push ( new HashEntry ( { key : head . key , value : head . value } ) ) ;
179
+ if ( head . key === key ) {
180
+ let node = head ;
181
+ previous . next = head . next ;
182
+ this . size -= 1 ;
183
+ const res = { key, value : node . value } ;
184
+ node = null ;
185
+ return res ;
174
186
}
187
+ previous = head ;
175
188
head = head . next ;
176
189
}
177
-
178
- // eslint-disable-next-line no-underscore-dangle
179
- const sll = this . _convertNodesToSLL ( nodes ) ;
180
-
181
- this . bucket [ index ] = sll ;
182
- // update the index with the lastest head value
183
- return { key : vals } ;
184
190
}
185
191
186
192
getSize ( ) {
@@ -197,7 +203,7 @@ class HashTable {
197
203
// ht.set('hello', 'I am a new value');
198
204
// // console.log(ht.bucket);
199
205
// ht.set('hell', 'Bad value');
200
- // ht.set('hello', 'I am a yet another value');
206
+ // // ht.set('hello', 'I am a yet another value');
201
207
// ht.set('yellow', 'I am yellow');
202
208
203
209
// // console.log(ht.get('hello'));
@@ -208,4 +214,7 @@ class HashTable {
208
214
// console.log(ht.remove('hello'));
209
215
// console.log(ht.bucket);
210
216
217
+ // console.log(ht.remove('yellow'));
218
+ // console.log(ht.bucket);
219
+
211
220
module . exports = HashTable ;
0 commit comments