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!

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 August 2026

Contest runs August 1 - 31. Complete challenges to climb the leaderboard!

Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In