April 2018
Beginner to intermediate
426 pages
10h 19m
English
The last method we will implement for the HashTable is the remove method, which is as follows:
remove(key) { const hash = this.hashCode(key); // {1} const valuePair = this.table[hash]; // {2} if (valuePair != null) { delete this.table[hash]; // {3} return true; } return false;}
To remove a value from the HashTable, first, we need to know what position we need to access, so we retrieve the hash using the hashCode function ({1}). We retrieve the valuePair stored in the hash position ({2}) and in case the valuePair is not null or undefined, we remove it using the JavaScript delete operator ({3}). We also return true if the removal was successful and false otherwise.