I want to find the most frequent and median values of a given array by using C++. I assume that I have a float array such as
float *LRArr=new LRArr[1000];
The array is filled by random float number.
std::default_random_engine generator;
generator.seed( rd() );
std::uniform_real_distribution<> distribution(0, 10);
for(int j=0;j<1000;j++)
{
LRArr[j]=distribution(generator)
}
Now I want to get the most frequent values in the array. But it take many time. Could you suggest to me the faster way to implemt it by C or C++? I assume I have the LRArr
such as
LRArr={0.1,1.2,6.5,6.5,4.5,6.5}
==>output is: 6.5 and median 5.5
This is my way:
float getMostFreq(float* LRArr;int sizeLRArr)
{
int count = 1;
int currentIndex = 0;
for (int i = 1; i < sizeLRArr; i++)
{
if (LRArr[i] == LRArr[currentIndex])
count++;
else
count--;
if (count == 0)
{
currentIndex = i;
count = 1;
}
}
mostFreq = LRArr[currentIndex];
return mostFreq;
}