Kodewick
Start learning →
Basic←→0 / 60

Class pet vote

#033BasicMapsLoops5 min

The class is voting for a class pet, and votes holds how many votes each pet got. Write top_pet(votes) to return the pet with the most votes. On a tie, pick the one that comes first alphabetically; if nobody voted (empty), return "".

Examples
top_pet({{"cat", 12}, {"dog", 15}, {"fish", 3}})→'dog'
top_pet({})→''
The functionstring top_pet(const map<string, int> &votes)
main.cpp
#include <map>
#include <string>
using namespace std;

string top_pet(const map<string, int> &votes) {
    string best = "";
    int most = -1;
    for (const auto &p : votes) {
        // your code here: if it has more than most, remember it
    }
    return best;
}
Ctrl + Enter runs too
Result

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