fork download
  1. #include <iostream>
  2. #include <string>
  3. //#include <array>
  4. #include <bitset>
  5. #include <iterator>
  6. #include <algorithm>
  7.  
  8.  
  9. std::string removeDuplicates(const std::string& str)
  10. {
  11. //Map for storing character count
  12. //std::array<bool, 256> ctable{ false };
  13. std::bitset<256> ctable;
  14.  
  15. std::string newstring;
  16. newstring.reserve(str.size());
  17.  
  18. //Only copy one character of each type to the new string
  19. std::copy_if(str.begin(), str.end(), std::back_inserter(newstring),
  20. [&ctable](const auto& value) {
  21. std::size_t i = static_cast<unsigned char>(value);
  22. bool has_occured = ctable[i];
  23. ctable[i] = true;
  24. return !has_occured;
  25. });
  26.  
  27. return newstring;
  28. }
  29.  
  30.  
  31. int main(void)
  32. {
  33. for (std::string line; std::getline(std::cin, line); )
  34. std::cout << removeDuplicates(line) << std::endl;
  35. }
Success #stdin #stdout 0s 3464KB
stdin
hello
this is easy
stdout
helo
this eay