// Returns the value of the mode of the array a
// a - array
// n - size
int mode(const int &a[], uint n)
{
uint frequency[];
int index[];
bool found;
uint k = 0; // Index for index array
ArrayResize(frequency, n);
ArrayResize(index, n);
ArrayInitialize(frequency, 0);
ArrayInitialize(index, 0);
// Cycle through initial array
for (uint i = 0; i < n; i++)
{
// Find the current element in the index array
found = false;
for (uint j = 0; j <= i; j++)
{
if (index[j] == a[i])
{
frequency[j]++;
found = true;
break;
}
}
// If the element wasn't found in the index array, add it to the index array
if (!found)
{
index[k] = a[i];
frequency[k] = 1;
k++;
}
}
return index[ArrayMaximum(frequency)];
}