PIXELBANKv8.2.1
Menu

Majority Element

Given an array nums, return the majority element (appears more than n/2 times). The majority element always exists.

Example:

Input:
3,2,3
Output:
3
Reasoning:
  • The input array is [3, 2, 3], which has a total of n=3n = 3 elements.
  • To find the majority element, we look for the element that appears more than n/2=3/2=1.5n/2 = 3/2 = 1.5 times.
  • The element 3 appears twice, which is more than 1.51.5 times, so it is the majority element.
  • The final output is the majority element, which is 3.

Constraints:

  • 1 <= len(nums) <= 5 * 10^4
  • -10^9 <= nums[i] <= 10^9
Editor

Test Results

0/0
Run code to see test results.