Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
Approach
- this algo works just check or sum
class Solution {
public int majorityElement(int[] nums) {
int count = 0, candidate = 0;
for (int num: nums) {
if (count == 0)
candidate = num;
count += (candidate == num) ? 1 : -1;
}
return candidate;
}
}