1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
var majorityElement = function(nums: number[]): number { let count = 0; let majority = nums[0];
for (let i = 0; i < nums.length; i++) { if (count === 0) { majority = nums[i]; } if (majority === nums[i]) { count++; } else { count--; } } return majority; };
|