Majority Element

Easy Arrays & Hashing AI round

nums is a list of whole numbers, n of them.

One number in it, the majority, appears more than n / 2 times: more often than all the other numbers put together. Every list given has one.

Return the majority.

Example 1

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

4 appears 3 times in a list of 5, more than half of it. 7 and 1 appear once each.

Example 2

Input: nums = [-6,-6,0,9,-6,9,-6]
Output: -6

-6 appears 4 times out of 7, and 4 is more than 3.5.

Example 3

Input: nums = [8]
Output: 8

Constraints

  • 1 ≤ n ≤ 2 × 10⁵
  • -10⁹ ≤ nums[i] ≤ 10⁹
  • One number appears more than n / 2 times

Watch the explanation

More Arrays & Hashing

All Arrays & Hashing problems