206. Majority Element
Easy
Boyer-Moore Voting
Array
Problem
Given an array nums of size n, return the majority element. The majority element appears more than floor(n / 2) times and is guaranteed to exist.
Examples
Example 1
Input: [3,2,3]
Output: 3
Example 2
Input: [2,2,1,1,1,2,2]
Output: 2
Example 3
Input: [1]
Output: 1
Constraints
� 1 <= nums.length <= 100000
� -1000000000 <= nums[i] <= 1000000000
� A majority element always exists.
Hints
?? Pair occurrences of different values and cancel them.
?? The element that survives the cancellation is the majority candidate.
Expected Complexity
Time: O(n)
Space: O(1)
Follow-up
Can you solve it in O(n) time and O(1) extra space?
Practice Notes
Attempts: 0
Time spent: 0min
Hints used: 0
JavaScript
00:00
Loading...
Case 1
Case 2
Case 3
Input
[3,2,3]Expected
3