Start learning free

LESSON 130 OF 289 · DICTIONARIES - CHALLENGES

Phone Book Lookup in C#: Reading a Dictionary Value

Practice Read the full explanation

Given a phone book phoneBook, a Dictionary from names to phone numbers, and a name, return the phone number of name.

Every name in the phone book is a key, and its value is that person's phone number, a string like "555-0142". The name you look up is always in the phone book.

 

Example 1:

Input: phoneBook = {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"}, name = "Ben"
Output: "555-0199"
Explanation: The key "Ben" maps to "555-0199".

Example 2:

Input: phoneBook = {"Max": "555-0100"}, name = "Max"
Output: "555-0100"
Explanation: The phone book has a single entry, and it is the one we look up.

Example 3:

Input: phoneBook = {"Tom": "555-0111", "Tim": "555-0111"}, name = "Tim"
Output: "555-0111"
Explanation: Tom and Tim share a phone number. We only need the value of the key "Tim".

 

Constraints:

  • phoneBook has at least one entry.
  • name is always a key of phoneBook.
  • Names are single words made of English letters, and they are all different. Two people can share a phone number.
  • Every phone number is a string of three digits, a dash and four digits, like "555-0142".
STEP 1 OF 1
    Ctrl + Enter to check

    C# · DICTIONARIES - CHALLENGES

    Phone Book Lookup in C#, explained

    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:

    KeyValue
    "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

    1. Take the key from the parameter name.
    2. Read the value stored under it with phoneBook[name].
    3. 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 a KeyNotFoundException.

    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 under key.
    • Use the variable itself as the key, not a quoted string.
    • A missing key throws KeyNotFoundException when you read it with the indexer.
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free