class Solution {
public:
int majorityElement(vector& nums) {
int len=nums.size();
int ans=0,co=0,i=0;
sort(nums.begin(),nums.end());
for(int i=0;i<len;i++)
{
co=count(nums.begin(),nums.end(),nums[i]);
if(co>(len/2))
{
ans=nums[i];
break;
}
}
return ans;
}
};
I have to return the element which appears more than floor(n/2) times.
Why I am getting TLE for the following testcase
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1…]