Easy
Majority Element
- Time limit
- 1 s
- Memory limit
- 256 MB
- Estimated
- 20 min
Description
Description
Given an integer array nums, return the majority element: the value that appears more than floor(n / 2) times, where n is the length of the array. The array is guaranteed to always have a majority element.
Examples
Example 1
- Input:
nums = [3, 2, 3] - Output:
3
Example 2
- Input:
nums = [2, 2, 1, 1, 1, 2, 2] - Output:
2
Example 3
- Input:
nums = [1] - Output:
1
Expected Function Behavior
Implement majorityElement(nums) and return the majority value.
Constraints
1 <= nums.length <= 100000-1000000000 <= nums[i] <= 1000000000- Exactly one value occurs more than
floor(nums.length / 2)times.
Sample cases
3Sample 1
Input
nums = [3,2,3]
Expected output
3
Sample 2
Input
nums = [2,2,1,1,1,2,2]
Expected output
2
Skills and languages
Patterns
- Majority Element Voting
Languages
- Python 3
Solve this problem
Create a free account to write, run and submit your solution.
- Write Python or Java in the browser, with your draft saved as you type
- Run your code on the examples as often as you like
- Submit to a sandboxed judge and see exactly which case failed
- Ask the AI mentor for a hint without being handed the answer