fork(2) download
  1. #include <map>
  2. #include <algorithm>
  3. #include <iostream>
  4.  
  5. int main()
  6. {
  7. int array[8] = {6,1,7,8,6,6,1,9};
  8.  
  9. // A map to store num => freq
  10. std::map <int, int> freq;
  11.  
  12. // A map to store freq(can be duplicate) => num
  13. std::multimap <int, int> freqCounts;
  14.  
  15. // Store num => frequency
  16. for (int i = 0 ; i < 8; i++)
  17. {
  18. freq[array[i]] += 1;
  19. }
  20.  
  21. // Now Store freq => num
  22. for(auto const & iter : freq)
  23. {
  24. freqCounts.insert (std::pair<int,int>(iter.second, iter.first));
  25. }
  26.  
  27. // Print in reverse order i.e. highest frequency first
  28. for (std::multimap<int,int>::reverse_iterator rit=freqCounts.rbegin(); rit!=freqCounts.rend(); ++rit)
  29. {
  30. std::cout << rit->second << " : " << rit->first << '\n';
  31. }
  32.  
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
6 : 3
1 : 2
9 : 1
8 : 1
7 : 1