Problem overview
PhoneBookLookup receives phoneBook, a Dictionary<string, string> from names to phone numbers, and a name. It returns that person's number. The name is always a key of the dictionary.
For {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"} and name = "Ben", the answer is "555-0199". Two people may share a number: with {"Tom": "555-0111", "Tim": "555-0111"} and "Tim", the answer is "555-0111". Keys must be unique, but values don't have to be.
Approach
A Dictionary finds a value from its key directly, without scanning every entry. In C# you read a value with square brackets: phoneBook[name]. The key goes inside the brackets as an expression. Here it is the variable name, without quotes. Writing phoneBook["name"] would look for a key spelled name.
It helps to picture the dictionary as a table of key and value pairs:
| Key | Value |
|---|---|
"Ana" | "555-0142" |
"Ben" | "555-0199" |
"Cara" | "555-0123" |
The lookup goes from the left column to the right one only. You give a key and get its value. The reverse question, who has number 555-0199, is not what a dictionary answers directly.
Step by step
- Take the key from the parameter
name. - Read the value stored under it with
phoneBook[name]. - Return it. The value type is
string, which matches the method's return type, so no conversion is needed.
The solution in C#
class Solution {
public string PhoneBookLookup(Dictionary<string, string> phoneBook, string name) {
return phoneBook[name];
}
}
That one line is the whole lesson. The indexer returns the value stored under the key, so there is nothing to loop over or compare.
Why a dictionary and not a list
You could store the phone book as a list of pairs and loop over it until the name matches. That works, but it takes longer as the book grows, and it needs a loop, a comparison and a way to return when nothing matches. A Dictionary is built for this question: it uses the key to jump to the value, and it keeps each key only once. That is also why the names in this lesson are all different. If you assign to the same key twice, the second value replaces the first.
What happens with a missing key
Reading a key that isn't in a Dictionary with the indexer throws a KeyNotFoundException. The lesson promises the name is always present, so this solution doesn't check. When a key might be missing, use TryGetValue or GetValueOrDefault, or test with ContainsKey first.
Keys are compared exactly, including upper and lower case. With the phone book above, phoneBook["ben"] throws, because the key is "Ben".
Testing
{"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"}with"Ben"returns"555-0199".{"Tom": "555-0111", "Tim": "555-0111"}with"Tim"returns"555-0111".- The first dictionary with
"ben"throws aKeyNotFoundException.
Reading a value in your own program
In a file of your own, you would add using System.Collections.Generic; at the top to use Dictionary. Then var book = new Dictionary<string, string> { {"Ana", "555-0142"} }; builds a book with one entry, and book["Ana"] reads it back.
Key takeaways
dictionary[key]reads the value stored underkey.- Use the variable itself as the key, not a quoted string.
- A missing key throws
KeyNotFoundExceptionwhen you read it with the indexer.
