#include <iostream>
#include <vector>
#include <unordered_set>
#include <string>
#include <algorithm>
std::vector<std::string> removeDupWordUnique(std::vector<std::string> strings) //O(n*log(n))
{
std::sort(strings.begin(), strings.end());
auto end = std::unique(strings.begin(), strings.end());
strings.erase(end, strings.end());
return strings;
}
std::vector<std::string> removeDupWordUnorderedSet(const std::vector<std::string>& strings) //O(n)
{
std::unordered_set<std::string> unique(strings.begin(), strings.end());
return std::vector<std::string>(unique.begin(), unique.end());
}
int main() {
std::vector<std::string> duplicated = {"car", "plane", "bicycle", "car", "boat", "bike", "plane", "ship", "boat"};
std::vector<std::string> cleared_by_unique = removeDupWordUnique(duplicated);
std::vector<std::string> cleared_by_hashset = removeDupWordUnorderedSet(duplicated);
std::cout<<"Original set: ";
for(auto& i: duplicated)
std::cout<<i<<", ";
std::cout<<"\n";
std::cout<<"Unique: ";
for(auto& i: cleared_by_unique)
std::cout<<i<<", ";
std::cout<<"\n";
std::cout<<"After hashset: ";
for(auto& i: cleared_by_hashset)
std::cout<<i<<", ";
std::cout<<"\n";
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8dW5vcmRlcmVkX3NldD4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGFsZ29yaXRobT4KCnN0ZDo6dmVjdG9yPHN0ZDo6c3RyaW5nPiByZW1vdmVEdXBXb3JkVW5pcXVlKHN0ZDo6dmVjdG9yPHN0ZDo6c3RyaW5nPiBzdHJpbmdzKSAvL08obipsb2cobikpCnsKICAgIHN0ZDo6c29ydChzdHJpbmdzLmJlZ2luKCksIHN0cmluZ3MuZW5kKCkpOwogICAgYXV0byBlbmQgPSBzdGQ6OnVuaXF1ZShzdHJpbmdzLmJlZ2luKCksIHN0cmluZ3MuZW5kKCkpOwogICAgc3RyaW5ncy5lcmFzZShlbmQsIHN0cmluZ3MuZW5kKCkpOwogICAgcmV0dXJuIHN0cmluZ3M7Cn0KCnN0ZDo6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ZWFyZWRfYnlfaGFzaHNldCkKCQlzdGQ6OmNvdXQ8PGk8PCIsICI7CglzdGQ6OmNvdXQ8PCJcbiI7CglyZXR1cm4gMDsKfQ==
Original set: car, plane, bicycle, car, boat, bike, plane, ship, boat,
Unique: bicycle, bike, boat, car, plane, ship,
After hashset: ship, boat, bicycle, bike, plane, car,