Junior — Senior
Finding the majority element in an array
livecode
Task condition
Input data: an array of integers called numbers, containing n elements. It is necessary to find the majority element – the element that occurs more than n/2 times. It is guaranteed that such an element exists.
Example 1: Input: numbers = [3,2,3] Output: 3
Example 2: Input: numbers = [2,2,1,1,1,2,2] Output: 2
Constraints: 1 ≤ n ≤ 50000 -10⁹ ≤ numbers[i] ≤ 10⁹ You need to implement an algorithm with a time complexity of O(n) and an additional space complexity of O(1).
class Solution {
public int majorityElement(int[] numbers) {
// Implementation
}
}
"""