Skip to content
BytePatterns

Majority Value

EasyArrays#counting#single-pass~15m

Problem

One value in a list occupies more than half of all positions. Return that value. You may assume such a value always exists, so you never have to report failure, and the list is never empty.

Examples

Input:  nums = [3, 3, 4]
Output: 3
Why:    3 fills two of the three slots, which is more than half
Input:  nums = [2, 2, 1, 1, 2]
Output: 2
Why:    the majority value is not required to sit together
Input:  nums = [9]
Output: 9
Why:    edge case, the only value trivially owns more than half the list

Hints

0 / 3

Stuck on the idea rather than the code? Majority Element covers it.