I\'m trying to devise an algorithm in the form of a function that accepts two parameters, an array and the size of the array. I want it to return the mode of the array and i
If you simply wish to count the number of occurences then I suggest you use a std::map
or std::unordered_map
.
If you're mapping a counter to each distinct value then it's an easy task to count occurences using std::map
as each key can only be inserted once. To list the distinct numbers in your list simply iterate over the map.
Here's an example of how you could do it:
#include
#include
Output:
Number 1 occurs 4 times
Number 2 occurs 3 times
Average is 1.42857