I'm stuck on how to write a function that finds the mode or modes of a set of integers contained in an array, using that array and its length as parameters. I've found multiple solutions online on how to find the mode of an array, but I'm trying solve this problem in the following way:
Let's say the original array contains (0, 0, 1, 5, 5, 5, 7, 7, 7). I want to iterate through the array with a loop that finds the highest frequency of any amount without storing the mode, and store these frequencies in another array, where in this case, the new array would have the values (1, 2, 1, 1, 2, 3, 1, 2, 3). By finding the max value in the second array, I would find the highest frequency, being 3 in this case. Then I want to iterate through the original array again, comparing the highest frequency with the counts of each of the values in that array, and where there is a match, I return that value, which is 5 and 7 in this example I'm giving. Given the criteria here, how would you write this function for finding the mode or modes of a given array? (You can assume the array has already been presorted in ascending order).
Edit: Here's my preliminary code. I'm up to the step where I find the frequencies of each integer in the original array and store them into another array.
void findMode(int array, int size){
int count = 1;
int freq[size];
freq[0] = count;
for (int pass = 1; pass < size; pass++){
if (array[pass] == array[pass-1]){
count++;
freq[pass] = count;
}
else{
count = 1;
freq[pass] = count;
}
}