Without using any extra space || Moore's algorithm || O(n) TC || O(1) SC


#1
int Solution::majorityElement(const vector<int> &A) 
{
    int index=0,count=1;
    for(int i=1;i<A.size();i++)
    {
        count+=(A[i]==A[index]?1:-1);
        if(count<=0)
        {
            index=i;
            count=1;
        }
    }
    return A[index];
}

// Time complexity:- O(n)
// Space complexity:- O(1)