Start learning free

LESSON 128 OF 281 · DICTIONARIES - CHALLENGES

Restock in C++: Updating a Count in an unordered_map

Practice Read the full explanation

A shop keeps its stock in inventory, an unordered_map from item names to how many of each item it has. A delivery brings amount more of item. Add amount to the count of item and return the updated dictionary.

Only the count of item changes: every other entry stays as it is. The order of the entries doesn't matter.

 

Example 1:

Input: inventory = {"apples": 5, "bananas": 2, "cherries": 10}, item = "bananas", amount = 3
Output: {"apples": 5, "bananas": 5, "cherries": 10}
Explanation: "bananas" had a count of 2, and 2 + 3 = 5. The apples and the cherries keep their counts.

Example 2:

Input: inventory = {"milk": 0}, item = "milk", amount = 12
Output: {"milk": 12}
Explanation: A count of 0 means the milk was sold out. The delivery brings it to 0 + 12 = 12.

Example 3:

Input: inventory = {"pens": 7, "paper": 3}, item = "pens", amount = 1
Output: {"pens": 8, "paper": 3}
Explanation: One more pen: 7 + 1 = 8.

 

Constraints:

  • item is always a key of inventory.
  • amount is at least 1.
  • Every count is 0 or more.
  • Item names are single lowercase words, and they are all different.
STEP 1 OF 2
    Ctrl + Enter to check

    C++ · DICTIONARIES - CHALLENGES

    Restock in C++, explained

    Problem overview

    A shop keeps its stock in inventory, an unordered_map<string, int> from item names to counts. The function restock receives the map, an item and an amount. It adds amount to the count of item and returns the updated map. Every other entry stays as it is, and the order of the entries doesn't matter because an unordered_map has no useful order anyway.

    For {"apples": 5, "bananas": 2, "cherries": 10}, item = "bananas" and amount = 3, the result is {"apples": 5, "bananas": 5, "cherries": 10}. A count of 0 is fine too: {"milk": 0} with 12 more milk becomes {"milk": 12}.

    Approach

    The expression inventory[item] doesn't only read a value. It gives access to the value stored under the key, so it can sit on the left side of an assignment like a variable. That makes inventory[item] += amount a complete update: the old count is read, amount is added, and the sum is stored back under the same key. No other entry is touched.

    Compare it with a plain assignment: inventory[item] = amount would replace the count, and starting from {"pens": 7, "paper": 3} it would leave the pens at 1 instead of 8. Only += keeps the old count in the sum.

    Step by step

    1. Add the amount. Use += on inventory[item]. The item is always already a key, so this changes an existing count.
    2. Return the map. The map was changed in place, so returning it hands back every entry, with only the count of item different.

    The solution in C++

    class Solution {
    public:
        unordered_map<string, int> restock(unordered_map<string, int>& inventory, string item, int amount) {
            inventory[item] += amount;
            return inventory;
        }
    };
    

    What to know about this code:

    • Missing keys start at zero. For a key that isn't in the map, operator[] inserts it with a default value, and for an int that value is 0. So inventory["kiwis"] += 4 on an empty map creates the entry "kiwis" with the count 4, and the map then has one entry. This lesson's item is always present, but the same line also works as "add to the count, starting from zero" in other programs.
    • The map is modified in place. The parameter is a reference, so the caller's map changes too. The function returns a copy of the same map.
    • Arithmetic on the count is plain int arithmetic. A signed overflow is undefined behaviour in C++, but the lesson's counts are small.

    Edge cases and common mistakes

    • Replacing instead of adding. inventory[item] = amount would store only the delivery and lose the old count. With {"pens": 7, "paper": 3}, "pens" and 1, the right answer is 8, not 1.
    • Writing the item in quotes. inventory["item"] targets a key spelled i, t, e, m and would add a new entry with the count of the amount.
    • A count of zero. A sold-out item still has an entry with the value 0, so += works on it like on any other count.
    • Looking at the order. An unordered_map doesn't promise an order, so don't depend on one when you print it.

    Testing

    • {"apples": 5, "bananas": 2, "cherries": 10}, "bananas", 3 gives apples 5, bananas 5, cherries 10.
    • {"milk": 0}, "milk", 12 gives milk 12.
    • {"pens": 7, "paper": 3}, "pens", 1 gives pens 8, paper 3.
    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