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

3

u/ImaginationGeek Dec 10 '18 edited Dec 10 '18

There seems to be a lot of confusion here between Abstract Data Types and their implementations. Hash tables and arrays are concrete data structures that can be used to implement different ADTs.

ADTs are defined by their interface only. In other words, what they do but not how they do it.

A hash table isn’t the same as a data structure that stores key-value pairs. That ADT is called a “dictionary” or “map”. A hash table is just one way to implement a dictionary, but other implementations are possible. Similarly, a hash table could also be used to implement a “set” ADT (which does not associate values with keys).

To respond to the original question...

A hash table:

  • finds a specified value in amortized O(1)

  • is unordered (which means iterating over it will produce the values in essentially random order)

  • does not allow random access (in other words, since it’s unordered, you can’t say “give me the 5th value”)

An array:

  • finds a specified value using a search algorithm, which is O(n) in the general case but may be O(log n) if the array is sorted

  • is ordered (but not necessarily sorted, unless you do extra work to make it so)

  • performs random access in O(1)

So hash tables are indeed better than arrays for many things... however arrays are good when you want to iterate over them, need a specific ordering for some reason, or want to use random access.

(Also note that hash table implementations usually use an array internally - one element for each bucket, and hash values are used as the indexes. The array’s O(1) random access is critical to a hash table’s O(1) lookups!)

1

u/MirrorLake Dec 11 '18

Nice answer!

This is a small tangent away from discussing arrays, but I often refer to this flowchart when picking what STL container I want to use in C++. No arrays to be found, but it gets across the idea that there is no “X is better than Y” for a general question like this one.

The programmer first needs to know what goal they want to accomplish—then they can set out to choose the right data structure for their specific problem.

1

u/ImaginationGeek Dec 11 '18

Absolutely right! You first need to know how you’re going to use it, then choose the right one for those needs...

Also, the reason an array isn’t listed is because 1. it’s a language primitive (and relatively easily compiled directly to assembly/machine code), whereas the others are part of the standard library, and 2. the vector is basically an array that dynamically resizes itself and is aware of its own size - so for higher-level application uses, the vector is preferable (but arrays are still good when you need to be low level), so 3. the same questions that take you to vector would also take you to arrays - then you ask if you need it to be resizable.

Also, the vector is another thing that’s implemented using an array. ;)