Majority Element
Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.

30:00

Majority Element
easy
Topics
Companies

Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.

Example 1:
Input: [2,2,1,1,1,2,2]
Output: 2
Constraints:
  • n==nums.lengthn == \text{nums.length}

  • 1n5×1041 \leq n \leq 5 \times 10^4

  • 109nums[i]109-10^9 \leq \text{nums}[i] \leq 10^9

  • The majority element always exists in the array.

Input
arr =[2,2,1,1,1,2,2]

Boyer-Moore Voting: Find the majority element

Candidate

-

Count

0

Array
2
2
1
1
1
2
2
Current
Candidate Value
Variables
No variables to display
DepthFunction Call
Stack empty
0/8