Open github-actions[bot] opened 2 years ago
// @lc code=start
/**
* @param {number[]} nums
* @return {number}
*/
var majorityElement = function (nums) {
if (nums.length === 1) return nums[0];
let count = 1;
let majority = nums[0];
for (i = 1; i < nums.length; i++) {
if (nums[i] !== majority) {
count--;
} else {
count++;
}
if (count == -1) {
majority = nums[i];
count = 0;
}
}
return majority;
};
// @lc code=end
169 Majority Element