Start learning free

LESSON 131 OF 667 · DICTIONARIES - CHALLENGES

Add a Contact to an unordered_map in C++

Practice Read the full explanation

You get a phone book phoneBook, an unordered_map from names to phone numbers. You also get a name and a phone number.

Add name with its number to the phone book, and return the phone book.

Only add name when it isn't in the phone book yet. A name that is already there keeps its old number. The order of the entries doesn't matter.

 

Example 1:

Input: phoneBook = {"Ana": "555-0142", "Ben": "555-0199"}, name = "Cara", number = "555-0123"
Output: {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"}
Explanation: "Cara" isn't in the phone book yet, so she is added.

Example 2:

Input: phoneBook = {"Ana": "555-0142", "Ben": "555-0199"}, name = "Ben", number = "555-0100"
Output: {"Ana": "555-0142", "Ben": "555-0199"}
Explanation: "Ben" is already in the phone book. He keeps his old number, "555-0199".

Example 3:

Input: phoneBook = {}, name = "Max", number = "555-0100"
Output: {"Max": "555-0100"}
Explanation: The phone book was empty. Now it has one contact.

 

Constraints:

  • phoneBook can be empty.
  • Names are single words made of English letters.
  • Every phone number has three digits, a dash and four digits, like "555-0142".
  • Two people can share a phone number.
STEP 1 OF 3
    Ctrl + Enter to check

    C++ · DICTIONARIES - CHALLENGES

    Add Contact in C++, explained

    Problem overview

    The method addContact gets phoneBook, an unordered_map<string, string> from names to phone numbers, plus a name and a phone number. It adds the name with its number and returns the phone book. But a name that is already in the book keeps its old number: only new names are added. The order of the entries does not matter.

    With {"Ana": "555-0142", "Ben": "555-0199"} and "Cara" with "555-0123", Cara is added. With "Ben" and "555-0100", nothing changes, and Ben keeps "555-0199". With an empty book and "Max", the book gets one entry.

    Approach

    Assigning with phoneBook[name] = number adds a missing key, but it also overwrites the value of an existing one. The task forbids the overwrite, so you must check first. phoneBook.count(name) is 0 when the name is not a key, and 1 when it is. Add the contact only in the 0 case.

    Step by step

    1. Write an if that checks phoneBook.count(name) == 0, and write its closing brace right away.
    2. Inside it, store the number under the name: phoneBook[name] = number. The key is new, so assigning adds it with its value.
    3. After the if, return phoneBook. It either has the new contact, or it is as it was.

    The solution in C++

    class Solution {
    public:
        unordered_map<string, string> addContact(unordered_map<string, string>& phoneBook, string name, string number) {
            if (phoneBook.count(name) == 0) {
                phoneBook[name] = number;
            }
            return phoneBook;
        }
    };
    

    An unordered_map has no fixed order, so a program that prints it may list the entries in any order. To compare results, sort them by key first. Running the method on the statement's examples, with entries printed as name: number lines sorted by name, gives:

    Ana: 555-0142
    Ben: 555-0199
    Cara: 555-0123
    

    For the second example, the book stays at two entries, Ana: 555-0142 and Ben: 555-0199. For the third, an empty book plus Max gives the single entry Max: 555-0100.

    Trying more inputs

    Add "Zoe" with "555-0111" to an empty book, then add "Zoe" again with "555-0222". The second call changes nothing, so Zoe keeps 555-0111. Then add "Yan" with the same number "555-0111". Both people are in the book, because two names may share a number. The final book has the entries Yan: 555-0111 and Zoe: 555-0111.

    The method changes the book you pass in, because the parameter is a reference, and it also returns a copy of it. Both have the same contents, so checking either one is fine. Keeping the check and the store in the method, rather than in the caller, means every caller gets the same rule: existing names are never overwritten.

    Complexity

    Time is O(1) on average for the lookup and the insert, since a hash map does not scan its entries. Space is O(1) extra.

    Edge cases and common mistakes

    • Skipping the check. Writing phoneBook[name] = number without the if replaces Ben's number with "555-0100", which breaks the second example.
    • Checking the number instead of the name. Two people may share a number. Only the name matters, because the name is the key.
    • Wrong comparison. count(name) > 0 inside the if would add only names that exist already.
    • Empty book. No special case is needed: count on an empty map is 0, so the contact is added.
    Keep going with C++500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free