What data structure could I use to find the phone number of the person to whom the name of the person is assigned?

What data structure could I use to find the phone number of a person with a person’s name?

+4
source share
2 answers

Assuming you only request a query with the person’s name, the best option would be to use an associative data structure. This is basically a data structure, usually implemented as a hash table or balanced binary search tree, which stores data as a key => value (or, in other words, as pairs (key, value)). You query the data structure with the key and return the corresponding value. In your case, the key will be the name of the person, and the value will be the phone number.

Instead of embedding a hash table or binary search tree for this yourself, check to see if your language has something similar already in its library, in most languages ​​these days. Python has a dict , perl hashes, Java and C # has a Map , and C ++ has a STL Map .

Something may seem a little more complicated if you have multiple values ​​for the same key (e.g. the same person with multiple phone numbers), but there are workarounds like using list / vector as the value or using A slightly different structure that supports multiple values ​​for the same key (e.g. STL multimap ). But you probably don't need to worry about that.

+2
source

An associative array, such as a hash table.

Indeed, everything that displays keys to values. The specific data structure will depend on the language you use (if you do not want to implement your own, in which case you have fluency).

+1
source

Source: https://habr.com/ru/post/1299682/


All Articles