0

I have seen many solutions to character counting and keeping track of the max. occurrence char using arrays and vectors but I am looking for implementation of map in c++ to do the same and I am stuck at the thing that map if defined like

map<int, int> mp; 
for (int i = 0; i < m; i++) 
     mp[a[i]]++;

So, If we write this then this will this give the keys as the count of the char if I used so, Is there a direct method or function which can find the max. value in map without looping over it.

map<char ,int> mp;

How can I use the map in c++ to find the max. occurrence of the char in the given string?

  • 3
    What's wrong with the code you show? – Some programmer dude Apr 22 '20 at 12:51
  • you found a solution and now you want to know if this really is a solution? Did you try to use it? – 463035818_is_not_an_ai Apr 22 '20 at 12:55
  • What charset are you working with? If it's just unsigned chars, your code will be faster by using an `std::array mp;` instead of `std::map mp;` (you can use it pretty much the same as the map in your scenario) – ypnos Apr 22 '20 at 12:55
  • 1
    keys are the characters (stored as `int`) and the values are the counts, otherwise the code does what you want. "or if not..." why do you have doubts? Your question is rather unclear – 463035818_is_not_an_ai Apr 22 '20 at 13:00
  • How can I go for the max count the char or simply how do I access that particular element with max. counts in maps.I am stuck how to hardcode or accessing the max. count values from the map I created . – MostLoved Potato Apr 22 '20 at 13:03
  • I tried to make your question a bit more clear, you should include a complete example of your code ([mcve]). Here is a related question: https://stackoverflow.com/questions/9370945/c-help-finding-the-max-value-in-a-map/9371137 – 463035818_is_not_an_ai Apr 22 '20 at 13:10
  • Oh , thanks that question cleared my doubts. – MostLoved Potato Apr 22 '20 at 13:14

0 Answers0