fork download
  1. #include <map>
  2. #include <vector>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <iterator>
  6.  
  7. using namespace std;
  8.  
  9. std::vector<int> lonelyinteger(const std::vector<int>& a)
  10. {
  11. std::vector<int> retValue;
  12. typedef std::map<int, int> IntMap;
  13. IntMap theMap;
  14.  
  15. // build the map
  16. for_each(a.begin(), a.end(), [&](int n){ theMap[n]++; });
  17.  
  18. // find all first entry with a count of 1
  19. for_each(theMap.begin(), theMap.end(),
  20. [&](const IntMap::value_type& pr)
  21. {if (pr.second == 1) retValue.push_back(pr.first); });
  22. return retValue;
  23. }
  24.  
  25. int main()
  26. {
  27. std::vector<int> TestVect = { 1, 1, 2, 3, 5, 0, 2, 8 };
  28. std::vector<int> ans = lonelyinteger(TestVect);
  29. copy(ans.begin(), ans.end(), ostream_iterator<int>(cout," "));
  30. }
  31.  
  32.  
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
0 3 5 8