Advertisement

Majority Element - LeetCode 169 Solution

Majority Element - Complete Solution Guide

Majority Element is LeetCode problem 169, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an array nums of size n , return the majority element . The majority element is the element that appears more than &lfloor;n / 2&rfloor; times. You may assume that the majority element always exists in the array. Example 1: Input: nums = [3,2,3] Output: 3 Example 2: Input: nums = [2,2,1,1,1,2,2] Output: 2 Constraints: n == nums.length 1 <= n <= 5 * 10 4 -10 9 <= nums[i] <= 10 9 Follow-up: Could you solve the problem in linear time and in O(1) space?

Detailed Explanation

The problem asks you to find the majority element in an array. The majority element is defined as the element that appears more than ⌊n/2⌋ times, where n is the length of the array. The problem guarantees that a majority element always exists within the input array. The input is an array of integers, and the output is a single integer representing the majority element.

Solution Approach

The provided solutions use different approaches. The Python solution uses a hash table (dictionary) to count the occurrences of each element. It iterates through the array, updating the count for each element. If an element's count exceeds ⌊n/2⌋, it's immediately returned. The Java, C++, and C solutions use the Boyer-Moore Voting Algorithm. This algorithm cleverly maintains a candidate element and a counter. When it encounters the candidate, it increments the counter; otherwise, it decrements the counter. If the counter reaches 0, it updates the candidate with the current element. After iterating through the entire array, the candidate element is the majority element.

Step-by-Step Algorithm

  1. Step 1: (Python) Initialize an empty dictionary to store element counts. (Boyer-Moore) Initialize a candidate element and a counter to 0.
  2. Step 2: (Python) Iterate through the array. For each element, increment its count in the dictionary. If the count exceeds ⌊n/2⌋, return the element. (Boyer-Moore) Iterate through the array. If the counter is 0, set the current element as the candidate. If the current element equals the candidate, increment the counter; otherwise, decrement it.
  3. Step 3: (Python) If the loop completes without finding a majority element (which shouldn't happen based on the problem statement), return -1. (Boyer-Moore) After iterating, the candidate element holds the majority element.

Key Insights

  • Insight 1: The problem can be solved efficiently without sorting the entire array. A linear time solution is possible.
  • Insight 2: The Boyer-Moore Voting Algorithm (used in the Java, C++, and C solutions) provides an elegant linear time, constant space solution.
  • Insight 3: Handling edge cases like empty arrays (though the problem statement guarantees a non-empty array with a majority element) is important in a robust solution. The Python solution handles this implicitly through the dictionary, but the other solutions would require explicit checks.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Divide and Conquer, Sorting, Counting.

Companies

Asked at: Accenture, Autodesk, CVENT, DE Shaw, Deloitte, Flipkart, IBM, MakeMyTrip, Media.net, Nvidia, Oracle, Pwc, Qualcomm, Salesforce, Swiggy, Yandex, Zenefits, eBay, tcs.