Majority Element

Brute Force | Using Map | 100% Success

int Solution::majorityElement(const vector<int> &A) {
    map<int,int>m;
    for(int i=0;i<A.size();i++){
        m[A[i]]++;
    }
    for(int i=0;i<A.size();i++){
        if(m[A[i]]>A.size()/2)
        return A[i];
    }
    return -1;
}

Comments

Popular posts from this blog

1431. Kids With the Greatest Number of Candies

125. Valid Palindrome

771. Jewels and Stones