All questions
EasyArrays · Boyer-Moore Voting
Majority Element
Written by a community member · · 1 attempt · 100% pass
0
Given an integer array nums of size n, return the majority element.
The majority element is the element that appears more than floor(n / 2) times. You may assume that a majority element always exists in nums.
Aim for an O(n) time solution. An O(1) extra-space solution is preferred.
Examples:
Input: nums = [3, 2, 3]
Output: 3
Input: nums = [2, 2, 1, 1, 1, 2, 2]
Output: 2