Grokking Data Structures & Algorithms for Coding Interviews
Vote
0% completed
Majority Element (easy)
Problem Statement
Given an array nums having an n elements, identify the element that appears the majority of the time, meaning more than n/2 times.
Examples
-
Example 1:
- Input:
[1, 2, 2, 3, 2] - Expected Output:
2 - Justification: Here, '2' appears 3 times in a 5-element array, making it the majority element.
- Input:
-
Example 2:
- Input:
[4, 4, 4, 4, 7, 4, 4] - Expected Output:
4 - Justification: '4' is the majority element as it appears 5 out of 7 times.
- Input:
-
Example 3:
- Input:
[9, 9, 1, 1, 9, 1, 9, 9] - Expected Output:
9
- Input:
.....
.....
.....
Like the course? Get enrolled and start learning!
Tuấn Trần
· 3 years ago
Reading your solution for this problem, I see that we have to re-scan the array chunk if two halves do not agree on one majority. So, I think we can just return the count result for each half, merge them at the last step and find the majority.
Show 1 reply