Kodewick
Start learning →
Mini challenge←→0 / 60

Best seller

#031Mini challengeMapsLoops7 min

The tea shop writes down the name of every drink it sells. Write best_seller(sales) to return the drink that sold the most. If two sold just as many, return the one that comes first alphabetically. At least one drink was sold.

Examples
best_seller({"latte", "tea", "latte"})→'latte'
best_seller({"mocha"})→'mocha'
The functionstring best_seller(const vector<string> &sales)
main.cpp
#include <map>
#include <string>
#include <vector>
using namespace std;

string best_seller(const vector<string> &sales) {
    map<string, int> count;
    for (const string &drink : sales) {
        // your code here: add 1 to this drink's count
    }
    string best = "";
    int most = 0;
    for (const auto &item : count) {
        // your code here: if it beats most, remember this drink and its count
    }
    return best;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.