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.
For example:
nums = [3,2,3]
should return 3
nums = [2,2,1,1,1,2,2]
should return 2
Write a function to find the majority element in an array. The function should be efficient and handle various input arrays, including cases where all elements are the same, and the array has only one element. What is the time and space complexity of your solution? Can you implement this in O(n) time and O(1) space?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks us to find the element that appears more than half the time in a collection of elements. The brute force method involves counting how many times each element appears in the entire collection, one by one, to see if it's the majority element.
Here's how the algorithm would work step-by-step:
def find_majority_element_brute_force(elements):
collection_size = len(elements)
for i in range(collection_size):
current_element = elements[i]
element_count = 0
# Count the frequency of the current element
for j in range(collection_size):
if elements[j] == current_element:
element_count += 1
# Check if current element is the majority
if element_count > collection_size / 2:
return current_element
# No majority element found
return None
The challenge is to find the number that appears more than half the time in a list. The optimal approach leverages a clever trick where we keep track of a 'candidate' number and a counter, effectively canceling out elements that aren't the majority.
Here's how the algorithm would work step-by-step:
def find_majority_element(number_list):
candidate_element = None
candidate_count = 0
for number in number_list:
if candidate_count == 0:
# If count is 0, assign current number as candidate.
candidate_element = number
candidate_count = 1
elif candidate_element == number:
candidate_count += 1
else:
candidate_count -= 1
# Verify the candidate is indeed the majority element.
candidate_frequency = number_list.count(candidate_element)
if candidate_frequency > len(number_list) // 2:
return candidate_element
else:
# No majority element exists
return None
Case | How to Handle |
---|---|
Empty input array | Return None or an appropriate error value/exception, as a majority element cannot exist. |
Array with a single element | Return that single element as it is the majority element by definition. |
Array with all identical elements | The algorithm should correctly identify this single element as the majority element. |
Large input array nearing memory limits | Consider using Boyer-Moore Voting Algorithm for its O(1) space complexity. |
Array with a majority element appearing only slightly more than n/2 times | Ensure the algorithm correctly identifies the element that exceeds the n/2 threshold, particularly when it's close. |
Integer overflow when counting occurrences (if using a naive approach) | Use a data type that can accommodate larger counts, or the Boyer-Moore algorithm. |
Negative numbers in the input array | The solution should work correctly regardless of the sign of the numbers, usually maps will handle this. |
No majority element exists | Return None or an appropriate error value/exception to indicate the absence of a majority element after iterating through the array. |