Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Find the mode of the numbers in a array

Hi there,
I need to write a java code to find the mode of a set of numbers (number i.e., repeated most number of times in a array). Any ideas/help is appreciated.....
Thanks in advance...
[203 byte] By [kreeksk] at [2007-11-11 10:28:10]
# 1 Re: Find the mode of the numbers in a array
sort the array
int max = 0;
int count = 0;
int sourceIndex = 0;
int maxIndex = 0;
int currentIndex = 0;
Type maxElement = null;
for( ; currentIndex < arraylength; ++currentIndex )
sourceIndex = currentIndex;
sourceElement = array[currentIndex];
while array[currentIndex] equals sourceElement
count++;
currentIndex++;
if count is larger than max
max = count;
maxIndex = sourceIndex;
maxElement = sourceElement;
count = 0;
return sourceIndex

Return sourceIndex if you want to know where the first occurrence of the element is, or sourceElement if you want to know the "value" of the element
nspils at 2007-11-11 22:31:27 >