Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times in the array. You may assume that the majority element always exists in the array.
Example 1:
Input: nums = [5,5,1,1,1,5,5]
Output: 5Example 2:
Input: nums = [2,2,2]
Output: 2Constraints:
1 <= nums.length <= 50,000-1,000,000,000 <= nums[i] <= 1,000,000,000Follow-up: Could you solve the problem in linear time and in O(1) space?
The majority element appears more than n/2 times. For each element, we can count how many times it appears in the array. If the count exceeds n/2, we've found our answer. This straightforward approach checks every element against every other element.
num in the array:num appears in the entire array.n / 2, return num.We can avoid repeated counting by using a hash map to store the frequency of each element as we iterate through the array. We track the element with the maximum count seen so far. Once any element's count exceeds n/2, it must be the majority element.
res and maxCount to track the current best candidate.num:maxCount, update res = num and maxCount = count[num].res.If we sort the array, the majority element must occupy the middle position. Since it appears more than n/2 times, no matter where the majority element's block starts, it will always include the index n/2. This gives us a simple one-liner solution after sorting.
n / 2.We can construct the majority element bit by bit. For each bit position, we count how many numbers have that bit set. If more than n/2 numbers have the bit set, then the majority element must also have that bit set. We build the result by combining all the majority bits.
1 to bit[i] if the i-th bit is set.bit[i] > n / 2, set that bit in the result.class Solution:
def majorityElement(self, nums: List[int]) -> int:
n = len(nums)
bit = [0] * 32
for num in nums:
for i in range(32):
bit[i] += ((num >> i) & 1)
res = 0
for i in range(32):
if bit[i] > (n // 2):
if i == 31:
res -= (1 << i)
else:
res |= (1 << i)
return resrepresents the number of bits as the given numbers are integers.
The Boyer-Moore algorithm works by maintaining a candidate and a count. When we see the candidate, we increment the count; otherwise, we decrement it. When the count reaches 0, we pick a new candidate. Since the majority element appears more than half the time, it will survive this elimination process and remain as the final candidate.
res as the candidate and count = 0.num:count == 0, set res = num.num == res, increment count; otherwise decrement count.res as the majority element.Since the majority element appears more than n/2 times, any random pick has greater than 50% chance of selecting it. We repeatedly pick a random element and check if it's the majority. On average, we need only about 2 picks to find the answer, making this surprisingly efficient in practice.
n / 2, return it.The probability of randomly choosing the majority element is greater than , so the expected number of iterations in the outer while loop is constant.
The majority element must appear more than n/2 times, not n/2 or more. Using count >= n/2 instead of count > n/2 can return incorrect results for arrays like [1, 2, 2] where 2 appears exactly n/2 times but is not a strict majority.
In languages like Python 3, / performs floating-point division while // performs integer division. Using n / 2 instead of n // 2 can lead to type errors or incorrect comparisons. Similarly, in JavaScript, Math.floor(n / 2) is needed for proper integer division.