Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋
times.
You may assume that the array is non-empty and the majority element always exist in the array.
int majorityElement(int* nums, int numsSize)
{
if(numsSize==1)
return nums[0];
int i,j,k;
for(i=1;i<numsSize;i++)
{
for(j=i-1;j>=0;j--)
{
if(nums[i]>=nums[j])
break;
}
if(i!=j-1)
{
int temp=nums[i];
for(k=i-1;k>j;k--)
{
nums[k+1]=nums[k];
}
nums[k+1]=temp;
}
}
int Time;
Time=numsSize/2;
int count=1;
for(i=0;i<numsSize-1;i++)
{
if(nums[i]==nums[i+1])
{
count++;
if(count>Time)
{
return nums[i];
}
}
else
{
count=1;
}
}
return 0;
}