Find Majority Element
Given an array of size 'n', find the element that appears more than n/2 times. This element is called the majority element. Assume that the array is non-empty and the majority element always exist in the array.
[ [ 3, 2, 3 ] ]
Explanation. Here, the number 3 appears 2 times which is more than half of the size of the array (3/2).
[ [ 2, 2, 1, 1, 1, 2, 2 ] ]
Explanation. The number 2 appears 4 times which is more than half of the array size (7/2).
[ [ 1 ] ]
Explanation. The array has only one element which is by default the majority element.
[ [ 1, 1, 2 ] ]
Explanation. The number 1 appears twice which is more than half of the array size (3/2).
Follow-up: Could you solve the problem in O(n) time and O(1) space complexity?
- The array will contain at least one element.\n- The majority element always exists and appears more than `n/2` times.\n- Input elements are integers.
- Views
- 2