Friday, July 31, 2009

C++ Find most occurence of an Array?

Find the value that occurs the most often. If there is a tie with multiple values having the most occurrences, any of them can be used. Such as if the array was 3 4 1 7 7 8 2 1 0 5 7 8 7 10 12. Then 7 would be returned as the most reoccurring.

C++ Find most occurence of an Array?
You could do this with a map%26lt;int,int%26gt;.





When you get a value not in the map, insert it with the value 1. If you get a key already in the map, then increment the value.





After all the inserting, iterate through the map and find the largest value and get that key.


No comments:

Post a Comment