📘
Majority Element
EasyArrays & Hashing
Given an array nums, return the majority element (appears more than n/2 times). The majority element always exists.
Example:
Input:
3,2,3
Output:
3
Reasoning:
- The input array is
[3, 2, 3], which has a total of n=3 elements. - To find the majority element, we look for the element that appears more than n/2=3/2=1.5 times.
- The element
3appears twice, which is more than 1.5 times, so it is the majority element. - The final output is the majority element, which is
3.
Constraints:
- 1 <= len(nums) <= 5 * 10^4
- -10^9 <= nums[i] <= 10^9
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.