Guest User

OddOccurrencesInArray

a guest
May 13th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.43 KB | None | 0 0
  1. #include <algorithm>
  2.  
  3. int solution(vector<int> &A) {
  4.     // write your code in C++11 (g++ 4.8.2)
  5.     std::sort(A.begin(), A.end());
  6.     if(A.size() <= 2 || A[0] != A[1])
  7.         return A.front();
  8.     for(auto it = A.begin() + 2; it < A.end()-2; it += 2)
  9.     {
  10.         if(*it != *(it+1))
  11.         {
  12.             if(*(it+1) != *(it+2))
  13.                 return *++it;
  14.             return *it;
  15.         }
  16.     }
  17.     return A.back();
  18. }
Advertisement
Add Comment
Please, Sign In to add comment