Find the element that appears more than half the time in an array.
Given an integer array nums, find the majority element — the value that appears more than ⌊n / 2⌋ times, where n is the length of the array.
You may assume that a majority element always exists.
Your task is to return that element.
nums.nums is an integer.⌊n / 2⌋ times.1 <= nums.lengthExample 1
Input
nums = [3,2,3]
Output
3
Explanation
The value 3 appears twice in an array of length 3, which is more than ⌊3/2⌋ = 1.
Example 2
Input
nums = [2,2,1,1,1,2,2]
Output
2
Explanation
The value 2 appears 4 times in an array of length 7, which is more than ⌊7/2⌋ = 3.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.