169. Majority Element
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find the "majority element" in an array. This is defined as the element that appears more than ⌊n / 2⌋ times. A crucial piece of information is that we can assume such an element always exists. The follow-up challenge is to solve this with O(n) time complexity and O(1) space complexity, which guides us toward an optimal solution.
Solution
Majority Element in Python
class Solution:
def majorityElement(self, nums: List[int]) -> int:
# Boyer-Moore Voting Algorithm
candidate = None
count = 0
for num in nums:
# If the count is 0, we set the current number as the new candidate.
if count == 0:
candidate = num
# We increment the count if the number is the same as the candidate,
# and decrement it otherwise.
if num == candidate:
count += 1
else:
count -= 1
# Since the majority element is guaranteed to exist, the final candidate
# will be the majority element.
return candidate
Majority Element in Java
class Solution {
public int majorityElement(int[] nums) {
// Boyer-Moore Voting Algorithm
int candidate = 0;
int count = 0;
for (int num : nums) {
// If count is 0, it means the previous candidate has been "cancelled out".
// We then select the current element as the new potential candidate.
if (count == 0) {
candidate = num;
}
// If the current number is the same as our candidate, we increment the count.
// If it's different, we decrement the count.
// This can be concisely written as a ternary operation.
count += (num == candidate) ? 1 : -1;
}
// The problem guarantees that a majority element always exists,
// so the final candidate is our answer.
return candidate;
}
}
Majority Element in C++
class Solution {
public:
int majorityElement(vector<int>& nums) {
// Boyer-Moore Voting Algorithm
int candidate = 0;
int count = 0;
for (int num : nums) {
// If count is 0, we choose the current element as the new candidate.
if (count == 0) {
candidate = num;
}
// If the current element is the same as the candidate, we increment the count.
// Otherwise, we decrement it.
if (num == candidate) {
count++;
} else {
count--;
}
}
// Because the majority element is guaranteed to exist, the final candidate
// will be the majority element. No second pass is needed for verification.
return candidate;
}
};
Majority Element in TypeScript
function majorityElement(nums: number[]): number {
// Boyer-Moore Voting Algorithm
let candidate: number | null = null;
let count = 0;
for (const num of nums) {
// If count is 0, we choose the current element as the new candidate.
if (count === 0) {
candidate = num;
}
// If the current element matches the candidate, increment count.
// Otherwise, decrement it.
count += (num === candidate) ? 1 : -1;
}
// The problem guarantees that a majority element always exists, so the candidate
// will hold the majority element at the end. The array is non-empty, so
// candidate will not be null. We use a non-null assertion `!`.
return candidate!;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.