I was just wondering how I would go about finding a mode of a unsorted array. I know that if it's sorted I can pass data into a 2 dimensional array and find the largest count.
I tried to write some code counting the first value, but it ends up giving me the wrong answer.
int getMode(int test[], int SIZE)
{
int currvalue = test[0];
int count = 1;
int countmax = 1;
int mode = test[0];
for (int i = 0; i < SIZE; ++i)
{
if (test[i] == currvalue)
++count;
else
{
if (count >= countmax)
{
countmax = count;
mode = test[i];
}
currvalue = test[i];
count = 2;
}
}
return mode;
}