+ 1

Why hashtables?

Why do hashtables have a function to retrieve hashes for different elements? Why not store the index of the last used index and just increment it by one? The index itself can then be passed, similar to hashes. So what I'm basically trying to say is, why use hashtables when you can use a simple array? Example: Before adding an element: Index | Status 1 | Used 2 | Not used [Add element to 2] After adding: [Save a value for the last used index (2)] Adding the next time: Increment last used index (saved as 2) by 1, 3 is the next usable index, add element and save new value Why is this simple way not better than using hashes?

5th Jul 2019, 4:33 PM
PirateOnThePlank
4 Answers
+ 3
When you have a large amount of data, it is very quick to retrieve any specific value from a hashtable, that is the advantage. https://www.sololearn.com/learn/668/?ref=app
5th Jul 2019, 7:01 PM
Tibor Santa
Tibor Santa - avatar
+ 3
Ok, so imagine a library with loads of books, but they are not organized, just put randomly on the shelves. You go in and want to find a specific book. Your best chance is to browse through all books, row by row, shelf by shelf, until you find it. That's how an array works. A library catalog is very similar to a hashtable index. It contains all the books and their locations. So you don't need to browse through all the books, just check the location from the catalog, and go fetch it. That's why it is faster.
6th Jul 2019, 5:14 AM
Tibor Santa
Tibor Santa - avatar
+ 1
But why is it quick?
6th Jul 2019, 2:57 AM
PirateOnThePlank
0
Hmm, but why not pass the index of the location and have it saved as an array instead of random hashes
6th Jul 2019, 4:42 PM
PirateOnThePlank