Best Fit For An Associative Array?

Which of the following sets of data would be the best fit to be stored in an associative array?

A list of cooking recipes A set of books sorted by genre A list of cities and their populations A set of countries grouped by continent

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Alex Chumbley
Aug 1, 2016

The first possible solution, a list of recipes, does not at all take advantage of the (key, value) structure of an associative array. It's just a list of strings. So, an array would be fine.

A list of cities grouped by continent and a list of books sorted by genre fall into the same basic category. That is, they are both sortable/groupable things. In essence, they're mapping more than one value (a country/book) to a key (a continent/genre). Now, this isn't necessarily a bad thing, but it doesn't take full advantage of the associative array. A typicaly question you might want to ask in this scenario might be "Is country X in continent Y?" This question will take O ( n ) O(n) time to answer because we've tacking on arrays of countries as our values.

A simple mapping between city and population is the best fit for an associative array. The query we'd be trying to answer here is "How many people does city X have?". This is an O ( 1 ) O(1) operation with an associative array. We map 1 key to 1 value, the keys are never repeated, and the values can be repeated. This is a classic use case for any key/value store.

Two cities could have roughly the same population.

Nicholas Kross - 3 years, 6 months ago

Log in to reply

Which means two different keys with same values, which is not a problem for associative arrays

Solotchi Veaceslav - 3 years ago

Log in to reply

So then why would book/genre be wrong?

GRIFTY P - 2 years, 10 months ago

Log in to reply

@Grifty P Maybe because a book can belong to more genres.

Solotchi Veaceslav - 2 years, 9 months ago

The examples are not well formulated and should be clarified. - "A list of cooking receipies" - If the receipes have unique names, one could use the names as key and the receipe itself as value. If just the list of names is given, I agree, a simple list would be sufficient. - "A set of books sorted by genre" - It is not clear, what should be the key and what should be the value. But anyway, neither books is a useful key (since a book might belong to more than one genre) nor genre is a good key (since more than one book might belong to a certain genre). - "A list of cities and their populations" - It only makes sense if city is used as key. But additionally, it has to be specified that the cities have unique names ("What is the population of Springfield?") - "A set of countries grouped by continent " - Continent cannot be used as key, since most continents contain more than one country. And to use country as key is also arguable since for example Russia belongs to two continents.

Tina Geweniger - 2 years, 2 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...