Majority Element
Given a non-empty integer array nums, return the element that appears more than floor(nums.length / 2) times. A majority element is guaranteed to exist.
Given a non-empty integer array `nums`, return the element that appears more than `floor(nums.length / 2)` times. A majority element is guaranteed to exist.
- `1 <= nums.length <= 5 * 10^4` - `-10^9 <= nums[i] <= 10^9` - A strict majority element always exists. - The interactive visualizer accepts at most 12 values; longer statement-valid inputs are execution-only.
Example 1: input nums = [3,2,3]; output 3. 3 appears twice, which is more than floor(3/2).
Example 2: input nums = [2,2,1,1,1,2,2]; output 2. 2 appears four times, which is more than half of seven.
Category: Arrays & Hashing. Topic: arrays. Difficulty: Easy. Patterns: hashing.