fork download
  1. #include <iostream>
  2. #include <utility>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. void count_equal_elements(vector<int>& vec, vector<pair<int,int> >& result){
  8. if (vec.empty())
  9. return;
  10. int curr = vec[0];
  11. int count = 1;
  12. for (vector<int>::iterator it = vec.begin()+1; it != vec.end(); ++it){
  13. if (curr == *it){
  14. count++;
  15. }
  16. else{
  17. result.push_back(make_pair(curr,count));
  18. curr = *it;
  19. count = 1;
  20. }
  21. }
  22. result.push_back(make_pair(curr,count));
  23. }
  24.  
  25. int main() {
  26. vector<int> input{1,1,2,3,3,3,1,1};
  27. vector<pair<int,int>> output;
  28.  
  29. count_equal_elements(input,output);
  30.  
  31. for (auto elem : output)
  32. cout << elem.first << "," << elem.second << endl;
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
1,2
2,1
3,3
1,2