Given a list of integers, find the majority element - the element that appears more than half the time (more than n/2 times where n is the list size). If no majority element exists, return null. Return null for null or empty input.

Examples:

Input: nums = [3, 3, 4, 2, 3, 3, 3] Output: 3 Explanation: 3 appears 5 times out of 7, which is more than 3 (7/2) Input: nums = [1, 2, 3] Output: null Explanation: No element appears more than 1 time, need more than 1 (3/2) Input: nums = [7] Output: 7 Explanation: Single element is always the majority Input: nums = null Output: null Explanation: Null input returns null Input: nums = [] Output: null Explanation: Empty list returns null
Apex Code Editor

Welcome to Lightning Challenge!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay Mar 2026

Contest runs March 3 - 31. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.