Dodomeki
Sign in

8. Majority Element

Easy

Given a non-empty array, return the value that appears in more than half of the positions. It is guaranteed that this value exists.

Example 1
Input: nums = [2,1,2,2,3,2,2]
Output: 2
Example 2
Input: nums = [-4,-4,8]
Output: -4
Constraints
  • 1 <= nums.length <= 10000
  • There exists a value with more than n / 2 occurrences.
Arrays
Caso 1
nums =
[2,1,2,2,3,2,2]
Expected =
2
Caso 2
nums =
[-4,-4,8]
Expected =
-4

Run checks these 2 cases. Submit runs all 6, including hidden ones.