I am needing to write an application that determines the value that comes up most often in an array in C++ and its position in the array.
For example, if I have an array A where
A[0] = 3 A[1] = 4 A[2] = 3
A[3] = 2 A[4] = 3 A[5] = -1
A[6] = 3 A[7] = 3
I must determine that 3 is the most often used number and am required to return the position in the array it occurs. Any position is fine. For example, 0,2,4,6,7 are all valid solutions.
Thoughts?
There are several ways to do this. One is to sort the array, so that you can count all similar values at once. Another is to use an associative array, a.k.a. a dictionary, where the values from A are keys and frequencies are the values associated with the keys.