fork(5) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <map>
  4. #include <unordered_map>
  5. using namespace std;
  6.  
  7. int main() {
  8. int array[]={100, 80, 90, 100, 80, 60, 80};
  9. const int number = sizeof(array)/sizeof(int);
  10. pair<int, size_t> result[number];
  11. int nres=0;
  12.  
  13. cout<<"Map approach: "<<endl;
  14.  
  15. // count each element
  16. unordered_map<int, size_t> count;
  17. for (int i=0; i<number; i++)
  18. count[array[i]]++;
  19. for (auto &e:count)
  20. cout << e.first <<" : "<<e.second<< "-> "<<e.first/e.second<<endl;
  21. cout<<endl;
  22.  
  23. // sorting approach
  24. std::sort(array, array + number);
  25. int counter;
  26. for(int i = 0; i < number; i+=counter) {
  27. for (counter=1;i+counter<number && array[i+counter]==array[i] ; )
  28. counter++;
  29. if (counter>1) {
  30. cout << "dup: " << array[i] << " "<<counter<<endl;
  31. result[nres++] = make_pair(array[i], counter);
  32. }
  33. }
  34. cout <<"---"<<endl;
  35. for (int i=0; i<nres; i++)
  36. cout << result[i].first <<" : "<<result[i].second<<" ->" << result[i].first/result[i].second<<endl;
  37.  
  38.  
  39.  
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Map approach: 
60 : 1-> 60
90 : 1-> 90
80 : 3-> 26
100 : 2-> 50

dup: 80 3
dup: 100 2
---
80 : 3 ->26
100 : 2 ->50