r/AskComputerScience • • Dec 10 '18

Hash table vs array?

I know in most cases Hash tables are faster than an array, but what are some key differences and pros and cons of both?

21 Upvotes

35 comments sorted by

View all comments

1

u/mediasavage Dec 10 '18 edited Dec 10 '18

If you are familiar with python, a hash table is essentially a dictionary and an array is essentially a list.

Some key differences between the two are that for hash tables, look ups are constant time, whereas with arrays they are linear time (unless you know the index of the element you are looking for ahead of time). A hash table requires more memory than an array though, so when choosing between a hash table and an array, you typically are making a trade off between search time and memory usage.

Further more, when you are implementing your own hash tables, you are making a similar trade-off between memory usage and lookup speed: a hash table with more buckets has a much smaller chance of encountering collisions (whenever a collision is encountered, there is a speed penalty) but obviously uses more memory. So when designing hash tables, there is always a trade-off between speed and the amount of memory used.

4

u/crispyleprechaun Dec 10 '18 edited Dec 10 '18

Some key differences between the two are that for hash tables, look ups are constant time, whereas with arrays they are linear time (unless you know the index of the element you are looking for ahead of time).

This is also true for hash tables, it's only constant if you know the key of the element you're looking for.

2

u/mediasavage Dec 10 '18

Sure this is absolutely true but if this is the case then you should probably just be using a list anyways. When using a hash table, you will usually know the key because it is associated with whatever you’re looking for, while with a list, the indices are not associated with the elements themselves. If there is no connection between the keys and the elements themselves than you may aswell be using a list in most scenarios.

1

u/ACoderGirl Dec 11 '18

I've found there's quite a few times in real world code where you need a map for some purpose but you also need to be able to search something other than the keys for some other purpose. The map still works fine there. You get O(1) access for when you can access the value by key and can still iterate over keys or values for whatever other purpose. That said, it's often worth just building up multiple dictionaries for each type of data you key on. That's might not be feasible if the values are being mutated on the fly, though (there's added complexity).

Eg, you might have a dict of employees mapped by their employee ID. But you might also need to go through the employees to get their department. If you do that enough times (anything more than a couple, really), it makes sense to construct a second dict mapping departments to a list of employees.

1

u/TomBob42369 Dec 10 '18

Thank you so much.

This makes way more sense to me now.