Code giving error on input with 1


  • 0
    S

    class Solution {
    public:
    int majorityElement(vector<int>& nums) {
    sort(nums.begin(),nums.end());
    int maxi=0,number=0,count1=0;
    int u=nums[0];
    int len=nums.size();
    if(len==1)
    cout<<u<<endl;
    else{
    for(int i=0;i<nums.size()-1;i++)
    {
    if(nums[i]==nums[i+1])
    count1++;
    else
    count1=0;
    if(count1>maxi)
    {maxi=count1;
    number=nums[i];
    }
    }
    if(maxi>(nums.size()/2))
    cout<<number<<endl;

        }
    }
    

    };


  • 0
    I

    Make your code easier to read, if you are asking for help.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.