fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_set>
  4. #include <string>
  5. #include <algorithm>
  6.  
  7. std::vector<std::string> removeDupWordUnique(std::vector<std::string> strings) //O(n*log(n))
  8. {
  9. std::sort(strings.begin(), strings.end());
  10. auto end = std::unique(strings.begin(), strings.end());
  11. strings.erase(end, strings.end());
  12. return strings;
  13. }
  14.  
  15. std::vector<std::string> removeDupWordUnorderedSet(const std::vector<std::string>& strings) //O(n)
  16. {
  17. std::unordered_set<std::string> unique(strings.begin(), strings.end());
  18. return std::vector<std::string>(unique.begin(), unique.end());
  19. }
  20.  
  21.  
  22. int main() {
  23. std::vector<std::string> duplicated = {"car", "plane", "bicycle", "car", "boat", "bike", "plane", "ship", "boat"};
  24. std::vector<std::string> cleared_by_unique = removeDupWordUnique(duplicated);
  25. std::vector<std::string> cleared_by_hashset = removeDupWordUnorderedSet(duplicated);
  26. std::cout<<"Original set: ";
  27. for(auto& i: duplicated)
  28. std::cout<<i<<", ";
  29. std::cout<<"\n";
  30. std::cout<<"Unique: ";
  31. for(auto& i: cleared_by_unique)
  32. std::cout<<i<<", ";
  33. std::cout<<"\n";
  34. std::cout<<"After hashset: ";
  35. for(auto& i: cleared_by_hashset)
  36. std::cout<<i<<", ";
  37. std::cout<<"\n";
  38. return 0;
  39. }
Success #stdin #stdout 0.01s 5296KB
stdin
Standard input is empty
stdout
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,