Calculating the statistical Mode(s) of a (possibly multi-modal) set of elements I am trying to come up with the most performant way of calculating the statistical Modes of a std::vector, obeying two conditions:

1) my function should not duplicate memory usage (which would required for most usually mentioned faster methods of calculating mode);

2) my function should be able to handle multi-modality. It means, it should be able to return more than one mode when apropriate (i.e. in case the elements in the passed vactor have more than one mode).

Here is my function so far. It works, but I have the feeling that it could be further improved and reviewed, so I brought it here:

#include &lt;vector&gt;
#include &lt;math.h&gt;

std::vector&lt;int&gt; Mode(std::vector&lt;int&gt; &amp;myvector)
{
int vectorsize = myvector.size();
int max = 0;
std::vector&lt;int&gt; maxs;
int counter = 1;
std::sort(myvector.begin(), myvector.end());

for (int i = 0; i &lt; vectorsize - 1; i++)
{
    int currentnum = myvector[i];

    if (currentnum == myvector[i + 1])
    {
        counter++;

        if (counter &gt; max)
        {
            if (counter &gt; vectorsize - i) break; //early exit when the mode found so far appears more than the number of elements still to be evaluated
            max = counter;
            if (maxs.size() &gt; 0) maxs.clear();
            maxs.push_back(currentnum);
        }
        else if (counter == max)
        {
            bool alreadyin = false;
            for (int j = 0; j &lt; maxs.size(); j++)
            {
                if (maxs[j] == currentnum)
                {
                    alreadyin = true;
                    break;
                }
            }
            if (alreadyin == false)
                maxs.push_back(currentnum);
        }

    }
    else counter = 1; //reset the counter
}

return (maxs.size() &gt; 0) ? maxs : myvector; //if all elements appear only once, return the whole initial vector; otherwise return the the modes that were found
}


Corrections, improvements an even suggestions of algorithm changes are very welcome.
So without really understanding what you mean with mode I would suggest the following purely based on your code:


Add includes. The code as is doesn't compile, which makes it harder for reviewers.
Premature Optimization. You are doing a lot of it. There is no reason to assign vectorsize to myvector.size(). The compiler will take care of that. Also currentnum is unnecessary.
Be consistent with your style. Putting else clauses at the same line with the code makes the code incredibly hard to read. Also parentheses really help in tracing the control flow. Generally nested control flow should utilize parentheses. 
Naming. Consider camelCase or scores, but do not just add lowercaseletters as in alreadyin
alreadyin == false should be !alreadyin


So know that I know what you mean we can optimize the code.

The first thing would be naming. The vector containing the modes should be named modes rather than maxs. Similarly I would rather name max as currentMode or better modeStrength.

The second point would be the update of the vector. Currently you always clear it. However, if it only has one mode you can just change that value.

if (modes.size() == 1) 
    modes[0] = myVector[i];
else if (modes.size() &gt; 1)  {
    modes.clear();
    modes.push_back(myVector[i]);
} else {
    modes.push_back(myVector[i]);
}


The third idea would be to increment your loop based on the found matches so you do not reiterate already processed data

unsigned counter;
unsigned modeStrength = 0;
for (unsigned i=0; i &lt; myVector.size() - 1; i += counter) {
    counter = 1;
    while ((counter + i != myVector.size()) &amp;&amp; 
           (myVector[i] == myVector[i+counter])) {
        counter++;
    }
    if (counter == modeStrength) {
        modes.push_back(myVector[i]);
    } else if (counter &gt; modeStrength) {
        modeStrength = counter;
        if (modes.size() == 1) {
            modes[0] = myVector[i];
        } else {
            modes.clear();
            modes.push_back(myVector[i]);
        }
    }
}


That would also turn the check for existence unnecessary.