Back to Practice Dashboard
DSA SectionEasy
Majority Element
Learn how to solve the 'Majority Element' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
Write a function find_majority_element(arr) that takes a list of integers arr and returns the majority element (the element that appears more than n // 2 times). You may assume that the majority element always exists in the array.
Constraints
- •1 <= len(arr) <= 5 * 10^4
- •-10^9 <= arr[i] <= 10^9
Examples
Example 1
Input
arr = [3, 2, 3]
Output
3
Explanation
3 appears twice, which is more than 3 // 2 = 1 time.
Example 2
Input
arr = [2, 2, 1, 1, 1, 2, 2]
Output
2
Explanation
2 appears 4 times, which is more than 7 // 2 = 3 times.
Need a Hint?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.