DSA SectionEasy

Majority Element

Detailed guide and Python implementation for the 'Majority Element' problem.

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?
Consider using Arrays-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.