I can't find the mode...?
Hi,
I am student taking a computer science course, so i was need some help. Anyway i need to find the Mode of a single dimension array. I am only allowed to find the mode using only the array that i pass into the method as a parameter. here is what i have done so far and it is not working, can you have a look and tell me where i am going wrong.
public static double findMode(int y[])
{
int mode = 0;
int counter=0, counter2=0;
for (int i=0;i<totalNumbers;i++)
{
for(int j=0; j<100; j++)
{
int temp = y;
if (temp == j)
{
counter++;
}
if(counter>counter2)
{
counter2=counter;
mode=temp;
}
}
}
return mode;
}
1 Attachment(s)
This one is quite flexible
I timed it to 4,3 sec on a 1.8gHz machine to find the mode in an array of
10000000 random integers with values varying from 1 to 1000, but I guess
this metod is just brute force..., I have attatched the source