fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9. string characters = "aasa asdfs dfh f ukjyhkh k wse f sdf sdfsdf";
  10.  
  11. // initialize with 256 entries, one for each character:
  12. vector<int> counts(256);
  13.  
  14. for (string::size_type i = 0; i <= characters.length(); i++) {
  15. // for each occurrence of a character, increase the value in the vector:
  16. int int_char = (int)characters[i];
  17. counts[int_char]++;
  18. }
  19. vector<int>::iterator most_frequent =
  20. std::max_element(counts.begin(), counts.end());
  21.  
  22. // getting the character (index within the container, "key"):
  23. cout << (char)(most_frequent - counts.begin()) << ": ";
  24.  
  25. // the number of occurrences ("value"):
  26. cout << (*most_frequent) << endl;
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
 : 9