Given an integer array of size n
, find all elements that appear more than ⌊ n/3 ⌋
times.
Example 1:
Input: nums = [3,2,3] Output: [3]
Example 2:
Input: nums = [1] Output: [1]
Example 3:
Input: nums = [1,2] Output: [1,2]
Constraints:
1 <= nums.length <= 5 * 104
-109 <= nums[i] <= 109
Follow up: Could you solve the problem in linear time and in 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 brute force approach to finding majority elements involves systematically checking each possible element to see if it appears more than a certain threshold. We essentially count the occurrences of every element and compare the count to the specified limit. It's like manually tallying votes for each candidate to see if anyone wins by a wide margin.
Here's how the algorithm would work step-by-step:
def majority_element_brute_force(numbers):
majority_elements = []
minimum_majority_count = len(numbers) // 3
for possible_majority_element_index in range(len(numbers)):
possible_majority_element = numbers[possible_majority_element_index]
element_count = 0
for number_index in range(len(numbers)):
if numbers[number_index] == possible_majority_element:
element_count += 1
# Need to determine if element is the majority
if element_count > minimum_majority_count:
is_duplicate = False
for already_found_element in majority_elements:
if already_found_element == possible_majority_element:
is_duplicate = True
break
# Avoid duplicates in the result
if not is_duplicate:
majority_elements.append(possible_majority_element)
return majority_elements
The problem requires us to find numbers that appear more than a third of the time in a list. The clever trick here is to realize that there can be at most two such numbers. We'll use a voting-based approach to identify potential candidates and then verify their counts.
Here's how the algorithm would work step-by-step:
def majorityElement(numbers):
candidate1 = None
candidate2 = None
candidate1_votes = 0
candidate2_votes = 0
for number in numbers:
if number == candidate1:
candidate1_votes += 1
elif number == candidate2:
candidate2_votes += 1
elif candidate1_votes == 0:
# Assign the number as a new candidate.
candidate1 = number
candidate1_votes = 1
elif candidate2_votes == 0:
# Assign the number as a new candidate.
candidate2 = number
candidate2_votes = 1
else:
# Decrement votes of both candidates.
candidate1_votes -= 1
candidate2_votes -= 1
actual_count1 = 0
actual_count2 = 0
for number in numbers:
if number == candidate1:
actual_count1 += 1
if number == candidate2:
actual_count2 += 1
result = []
if actual_count1 > len(numbers) // 3:
# Verify candidate1's occurence.
result.append(candidate1)
if candidate1 != candidate2 and actual_count2 > len(numbers) // 3:
# Verify candidate2's occurence.
result.append(candidate2)
return result
Case | How to Handle |
---|---|
Empty input array | Return an empty list because no elements can appear more than n/3 times. |
Array with one element | Return the single element as it appears more than n/3 (which is 0) times. |
Array with two elements, both identical | Return the single element, as it appears twice, which is more than n/3 (2/3 which truncates to 0). |
Array with two elements, both distinct | Return an empty list since neither element appears more than n/3 (0) times. |
Array with all elements identical | Return the single unique element, as it is the majority element. |
Large array with extreme positive and negative values | Moyer's Voting Algorithm handles both large positive and negative values without integer overflow assuming the counts are stored in a data type that can accomodate the array size. |
Array with elements clustered around a few values but no strict majority | Moyer's Voting Algorithm finds candidates, and a final validation step checks if their counts exceed n/3. |
Array containing zeros | The algorithm should correctly count zeros like any other integer element. |