Practice/Microsoft/Leetcode 169. Majority Element
CodingMust
You are given an array of integers where one element appears more frequently than all others combined. Specifically, there exists an element that appears more than ⌊n/2⌋ times, where n is the length of the array. Your task is to identify and return this dominant element.
You are guaranteed that such an element always exists in the input array.
Example 1:
Input: [3, 3, 4, 2, 3, 3, 3] Output: 3 Explanation: The element 3 appears 5 times in an array of length 7. Since 5 > 7/2, element 3 is dominant.
Example 2:
Input: [1] Output: 1 Explanation: In a single-element array, that element is always dominant.
Example 3:
Input: [2, 2, 1, 1, 1, 2, 2] Output: 2 Explanation: Element 2 appears 4 times and element 1 appears 3 times. Since 4 > 7/2, element 2 is dominant.