All problems

Majority Element

Easy · hash map

You are given a list of integers nums. One value in the list is the majority: it appears in strictly more than half of the positions. In a list of 7 numbers, for example, the majority value appears at least 4 times.

Return the majority value. The input always contains one, and there can only ever be one.

Examples

Example 1

Input: nums = [5, 2, 5, 5, 3]

Output: 5

5 appears 3 times out of 5, which is more than half.

Example 2

Input: nums = [8]

Output: 8

A list with one number is its own majority.

Example 3

Input: nums = [1, 4, 4, 1, 4, 1, 1]

Output: 1

1 appears 4 times out of 7. The 4s appear 3 times, which is not enough.

Constraints