Kodewick
Start learning →
Mini challenge←→0 / 60

No-repeat playlist

#044Mini challengeClassesvector6 min

Build a Playlist. Write add(song): if the playlist already has that song (exactly the same name), don't add it; otherwise put it at the end. playlist_size adds the songs one by one and returns how many the playlist has.

Examples
playlist_size({"Yellow", "Hello", "Yellow"})→2
playlist_size({})→0
The functionint playlist_size(const vector<string> &adds)
main.cpp
#include <string>
#include <vector>
using namespace std;

class Playlist {
public:
    vector<string> songs;

    void add(const string &song) {
        // your code here: add to songs only if it isn't there
    }
};

int playlist_size(const vector<string> &adds) {
    Playlist p;
    for (const string &s : adds) {
        p.add(s);
    }
    return p.songs.size();
}
Ctrl + Enter runs too
Result

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