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<iostream>
#include<map>
int main() {
int test_cases{};
std::cin >> test_cases;
for (auto i = 0; i < test_cases; ++i) {
std::map<int,int> count;
std::size_t size{};
std::cin >> size;
int x{};
for (std::size_t i = 0; i < size; ++i) {
std::cin >> x;
count[x] = ++count[x];
}
int number{};
int numberCount{};
for (auto& i : count) {
if (i.second > numberCount) {
number = i.first;
numberCount = i.second;
} else if (i.second == numberCount) {
if (i.first < number) {
number = i.first;
}
} else {
continue;
}
}
std::cout << number << " " << numberCount << '\n';
}
}
How can I make this better and faster?