Cod sursa(job #1836170)

Utilizator Corina203Corina Corina203 Data 27 decembrie 2016 22:09:16
Problema Elementul majoritar Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <iostream>
#include <fstream>
#include <map>

using namespace std;

int main() {
    int n;
    int number;
    map<int, int> apparitions;
    int max = -1;
    int element = -1;

    ifstream input("elmaj.in");
    ofstream output("elmaj.out");

    input >> n;
    while (n) {
        input >> number;
        map<int, int>::iterator index = apparitions.find(number);

        if (index == apparitions.end()) {
            apparitions.insert(pair<int, int>(number, 1));
            continue;
        }

        index->second++;
        if (index->second > max) {
            element = number;
            max = index->second;
        }
        n--;
    }
    if (element == -1)
        output << element;
    else
        output << element << " " << max;
    return 0;
}