Back to course home
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!