Kodewick
Start learning →
Mini challenge←0 / 60

Tidy shopping list

#060Mini challengeAlgorithmsvector7 min

The whole family adds to the shopping list, and some things end up on it twice. Write tidy_list(items): the starter has already sorted it with sort; put each thing into tidy just once. Different capitals count as different things.

Examples
tidy_list({"milk", "eggs", "milk", "bread"})→['bread', 'eggs', 'milk']
tidy_list({})→[]
The functionvector<string> tidy_list(vector<string> items)
main.cpp
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

vector<string> tidy_list(vector<string> items) {
    sort(items.begin(), items.end());
    vector<string> tidy;
    for (const string &item : items) {
        // your code here: add it only if it differs from the last in tidy
    }
    return tidy;
}
Ctrl + Enter runs too
Result

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