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