Count of Maximum - CodeChef I'm doing an easy problem in codechef. Here's the problem statement for maxcount:


  Given an array A of length N, your task is to find the element which
  repeats in A maximum number of times as well as the corresponding
  count. In case of ties, choose the smaller element first.


OK, I cheated. I used std:map because I want to play with it. Please review my code:

#include&lt;iostream&gt;
#include&lt;map&gt;

int main() {
    int test_cases{};
    std::cin &gt;&gt; test_cases;
    for (auto i = 0; i &lt; test_cases; ++i) {
        std::map&lt;int,int&gt; count;
        std::size_t size{};
        std::cin &gt;&gt; size;
        int x{};
        for (std::size_t i = 0; i &lt; size; ++i) {
            std::cin &gt;&gt; x;
            count[x] = ++count[x];
        }
        int number{};
        int numberCount{};
        for (auto&amp; i : count) {
            if (i.second &gt; numberCount) {
                number = i.first;
                numberCount = i.second;
            } else if (i.second == numberCount) {
                if (i.first &lt; number) {
                    number = i.first;
                }
            } else {
                continue;
            }
        }
        std::cout &lt;&lt; number &lt;&lt; " " &lt;&lt; numberCount &lt;&lt; '\n';
    }
}


How can I make this better and faster?
Simplify:

        count[x] = ++count[x];

        // Why not just;
        ++count[x];


Variable name length:

for (auto i = 0; i &lt; test_cases; ++i) {


Have you ever tried searching for all occurrences of the variable i in the resulting loop. The number of false positives will be a pain in the arse. Name your loop variables so you can find them easily.

Hiding variables names:

    for (std::size_t i = 0; i &lt; size; ++i) {


Though not technically illegal. This becomes a maintenance problem. It's OK for you today as you just wrote the code. But for anybody else (or you in years time) this is can be a pain. Try and give your variables unique meaningful names (self documenting code is a brilliant practice but it requires variable names to be meaningful).

The whole loop where you search for the largest repeat:

    for (std::size_t i = 0; i &lt; size; ++i) {


This can be done inline while you were counting. I would not have a second loop to work it out after the fact.

I would simplify this condition:

} else if (i.second == numberCount) {
            if (i.first &lt; number) {

// I find it easier to read as:

} else if (i.second == numberCount) &amp;&amp; (i.first &lt; number) {


This seems a bit redundant:

        } else {
            continue;
        }


Personally I think initializing integers with {} looks terrible (and is slightly confusing).

std::size_t size{};

std::size_t size = 0;  // Much easier to read.


Whats the point in initizliaing a variable just before you write over it?

    std::size_t size{};   // Why initialize it here
    std::cin &gt;&gt; size;     // Only to trash the initialization here.


Address Comments:

    int number{};
    int numberCount{};
    for (std::size_t i = 0; i &lt; size; ++i) {

        std::cin &gt;&gt; x;
        std::size_t&amp;  countV = count[x];

        ++countV;

        if (countV &gt; numberCount || (countV == numberCount &amp;&amp; x &lt; number))
        {
            number      = x;
            numberCount = countV;
        }
    }