DEV Community

Discussion on: Daily Challenge #220 - What Dominates Your Array?

Collapse
 
vidit1999 profile image
Vidit Sarkar

C++ solution

int dominator(vector<int> arr){
    // stores the number with its count
    unordered_map<int, int> numCount;

    // if count of any number becomes more than arr.size()/2
    // then return that number
    for(int num : arr){
        if(++numCount[num] > arr.size()/2)
            return num;
    }

    // if no number has count more than arr.size()/2
    // then return -1
    return -1;
}