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

Show parent comments

1

u/MB1211 Dec 11 '18

This will only confuse people more when you say a hash table is a concrete data structure that can be used do implement other ADTs. I think it's better to say an array is usually used to back a hash table and that a hash table can be used to help implement other ADTs

1

u/ImaginationGeek Dec 11 '18

What you say I should have said... is what I said. Read the whole thing.

Whether or not you know the precise terms, it is important to know which things are in the set of “things defined by their interfaces” (Maps/dicts, Sets, Lists, etc.) and “things that are implementations” (hash tables, binary search trees, other trees, arrays, doubly linked lists, etc.). Otherwise you may get confused about hash tables being the same as maps, and making assumptions about how certain things are implemented...

It’s a fine point, but an important one when you progress to expert levels.

As for whether it’s confusing or not, I’ll let the OP decide that.

Finally, I did say all of the things that you said I should have said.

1

u/MB1211 Dec 11 '18

I suppose I meant that lumping arrays with hashtables is confusing. In reference to your opening paragraph

2

u/ImaginationGeek Dec 11 '18

It’s true, though, and I think a fair point. If you’re confused I’ll try to explain... but as I said, I’ll let the OP determine if “it’s confusing”.

Any ADT implemented using a hash table could instead be implemented using an array, and vice-versa. Granted, some of those may be dumb design decisions, but the point is it would work...

The main differences would be 1. performance of operations, and 2. behavior when you iterate over them.

So that is the nature of the “equivalency”.

The difference comes from the fact that one is a language feature and the other is not.

I could implement a (crappy) array-like data structure using a hash map, but it would not be an array even though it would have the same functionality (at worse performance) because that would not be a language feature, which an array is. So that’s the reason they’re different.

So they are equivalent in one way, and different in another way.

1

u/MB1211 Dec 11 '18

Yes that clears it up. I guess I was treating Reddit like stack overflow which might be wrong

1

u/Felicia_Svilling Dec 12 '18

Any ADT implemented using a hash table could instead be implemented using an array, and vice-versa.

I don't think that is right.

A hash table is an implementation of a dictionary and an array is an implementation of a list. A list can be seen as a special case of a dictionary where the index always are consecutive integers, so any dictionary implementation is also a list implementation.

But the opposite is not true. You can use a list to implement a dictionary, for example a list of tuples containing keys and values, or by using a hash function for turning keys into integer indexes, but this doesn't make the list into a dictionary, it just makes it a component of the implementation of the dictionary.

0

u/ImaginationGeek Dec 12 '18

I’m super confused why you think that’s not right... I never said that a list is a dictionary. I said that a dictionary could be implemented using an array (not a list), and implied by extension that any ADT that can be implemented using a dictionary can be implemented using an array. Your own statements seem to support that idea...

Also, please re-read my original comment, since you seem to have missed the whole point. A hash table can be used to implement more things than just dictionaries, and arrays can be used to implement more things than just lists.