Cod sursa(job #1836160)

Utilizator Corina203Corina Corina203 Data 27 decembrie 2016 21:55:31
Problema Elementul majoritar Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 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;
        if( apparitions.find(number) == apparitions.end() )
        {
            apparitions.insert(pair<int,int>(number,1));
        }
        else
        {
            map<int,int>::iterator index = apparitions.find(number);
            index->second++;
            if(index->second > max)
            {
                element = number;
                max = index->second;
            }
        }
        n--;
    }
    if(element == -1 )
        output << element;
    else
        output << element << " " << max;
    return 0;
}