Majority Element
Easy · Arrays & Hashing · Array · Hash Table · Divide and Conquer
Problem
One value in nums appears more than len(nums) // 2 times. Return it. You may assume such a value always exists.
Examples
Input: nums = [3,2,3] Output: 3
3 appears twice out of three.
Input: nums = [2,2,1,1,1,2,2] Output: 2
2 appears four times out of seven.
Constraints
- 1 <= len(nums) <= 5 * 10^4
- -10^9 <= nums[i] <= 10^9
- A majority element always exists
Approaches
- Counter — time O(n), space O(n)
Related problems
Start free on Cariara · Coding problems · System design studio