Skip to content

Commit 46daaf5

Browse files
author
JD Medina
authoredDec 11, 2020
Modify HashTable (#447)
Add a getValues() method to the HashTable data structure
1 parent c3d2295 commit 46daaf5

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
 

‎src/data-structures/hash-table/HashTable.js

+12
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,16 @@ export default class HashTable {
105105
getKeys() {
106106
return Object.keys(this.keys);
107107
}
108+
109+
/**
110+
* Gets the list of all the stored values in the hash table in the order of
111+
* the keys map.
112+
*
113+
* @return {*[]}
114+
*/
115+
getValues() {
116+
const keys = this.getKeys();
117+
118+
return keys.map(key => this.buckets[this.hash(key)].head.value.value);
119+
}
108120
}

‎src/data-structures/hash-table/__test__/HashTable.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,14 @@ describe('HashTable', () => {
8686
expect(hashTable.has('b')).toBe(true);
8787
expect(hashTable.has('x')).toBe(false);
8888
});
89+
90+
it('should get all the values', () => {
91+
const hashTable = new HashTable(3);
92+
93+
hashTable.set('a', 'alpha');
94+
hashTable.set('b', 'beta');
95+
hashTable.set('c', 'gamma');
96+
97+
expect(hashTable.getValues()).toEqual(['alpha', 'beta', 'gamma']);
98+
});
8999
});

0 commit comments

Comments
 (0)
Please sign in to comment.