DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
easy
Array

Majority Element

**Problem Statement:** 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. **Hint / Expected Approach:** Boyer-Moore Voting Algorithm **Edge Cases to Consider:** - (1) Single element - (2) All same elements - (3) Majority at very end

Examples

Example 1
Input: [3,2,3]
Output: 3
Example 2
Input: [2,2,1,1,1,2,2]
Output: 2
Example 3
Input: [1]
Output: 1

Constraints

  • ▪1 <= nums.length <= 10^4
  • ▪-10^9 <= nums[i] <= 10^9
  • ▪Time complexity should be O(n) or O(n log n)

Watch Out For Edge Cases

  • ▪Single element
  • ▪All same elements
  • ▪Majority at very end
Frequently Asked At
AmazonGoogleMicrosoftMetaFlipkartWipro