In your original code and the code mathijs posted, once you get to the end of the loop you don't check the last value for having more occurrences. So for example if the vector only has 1 element, you go into the loop, add 1 to the count then exit the loop without checking if 1 > max_cnt. To fix that you'd need to add another identical check after the loop to do the final test. Or change the design into something like Unimportants.