LeetCode 169, Easy. Topics: Array, Hash Table, Divide and Conquer, Sorting, Counting. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 15 parametrized pytest cases, and a playground notebook:
Problem
Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
Examples
Constraints
- n == nums.length
- 1 <= n <= 5 * 10^4
- -10^9 <= nums[i] <= 10^9
Follow-up: Could you solve the problem in linear time and in O(1) space?
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Grind 75, Grind, NeetCode 250, NeetCode All, AlgoMaster 75. Last modified on September 7, 2026