跳至主要內容

169. Majority Element


169. Majority Elementopen in new window

🟢   🔖  数组 哈希表 分治 计数 排序  🔗 LeetCodeopen in new window

题目

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.

Example 1:

Input: nums = [3,2,3]

Output: 3

Example 2:

Input: nums = [2,2,1,1,1,2,2]

Output: 2

Constraints:

  • n == nums.length
  • 1 <= n <= 5 * 10^4
  • -10^9 <= nums[i] <= 10^9

Follow-up: Could you solve the problem in linear time and in O(1) space?

题目大意

给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。你可以假设数组是非空的,并且给定的数组总是存在众数。要求线性时间复杂度和 O(1) 空间复杂度。

解题思路

这道题如果用一个哈希表作为计数器,记录每个元素出现的次数,然后寻找出现次数最多的那个元素,时间和空间复杂度都是 O(N)

想要空间复杂度为 O(1) ,可以用栈的思路:遍历数组,如果栈为空,或者当前元素与栈顶元素相同,则入栈;否则将栈顶元素弹出。

由于数组中的众数出现次数大于 n/2 ,所以经过不断地入栈出栈,互相消除,最终栈内留下的肯定是我们要找的众数。证明如下:

  • 当数组内只有两种元素时,由于众数的数量过半,所以不管顺序如何,经过两两对比,不一样的互相消除,最后栈内留下的都是众数;
  • 当数组内有两种以上的元素时,除众数以外的其他元素还会互相对比消除,所以用于消耗众数的元素数量更少,所以栈内留下的元素就是众数。

实际上我们并不需要真的实现一个栈,只需要记录栈顶元素 target (栈内所有元素一定相同,遇到不相同的不会入栈),以及元素的个数 count ,因此只需要两个变量就够了。

代码

/**
 * @param {number[]} nums
 * @return {number}
 */
var majorityElement = function (nums) {
	let target;
	let count = 0;
	for (let item of nums) {
		if (count === 0 || item === target) {
			count++;
			target = item;
		} else {
			count--;
		}
	}
	return target;
};

相关题目

相关题目
- [229. 多数元素 II](https://leetcode.com/problems/majority-element-ii)
- [🔒 Check If a Number Is Majority Element in a Sorted Array](https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array)
- [2404. 出现最频繁的偶数元素](https://leetcode.com/problems/most-frequent-even-element)
- [2780. Minimum Index of a Valid Split](https://leetcode.com/problems/minimum-index-of-a-valid-split)