Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times.
class Solution {
public int majorityElement(int[] nums) {
int count = 0 ;
// means the number of target
int target = 0 ;
for(int i = 0 ;i<nums.length;i++)
{
if(count==0)
{
count++;
target=nums[i];
}
else
{
if(nums[i]==target)
count++;
else
count--;
}
}
return target;
}
}