|
| 1 | +# [Majority Element](https://leetcode.com/problems/majority-element/) |
| 2 | + |
| 3 | +## Question Description |
| 4 | +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. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## Constraints |
| 9 | +- `1 <= nums.length <= 5 * 10^4` |
| 10 | +- `-10^9 <= nums[i] <= 10^9` |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## Approach 1: Brute Force (Nested Loops) |
| 15 | + |
| 16 | +Use nested loops to count occurrences of each element. For each element, scan the entire array to count how many times it appears. If any element appears more than n/2 times, return it. |
| 17 | + |
| 18 | +**Time Complexity:** O(n²) - for each element, we scan the entire array |
| 19 | +**Space Complexity:** O(1) - no extra data structures used |
| 20 | + |
| 21 | +```java |
| 22 | +class Solution { |
| 23 | + public int majorityElement(int[] nums) { |
| 24 | + for (int i = 0; i < nums.length; i++) { |
| 25 | + int count = 0; |
| 26 | + for (int j = 0; j < nums.length; j++) { |
| 27 | + if (nums[i] == nums[j]) count++; |
| 28 | + } |
| 29 | + if (count > nums.length / 2) return nums[i]; |
| 30 | + } |
| 31 | + return -1; |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +## Approach 2: Sorting |
| 39 | + |
| 40 | +Sort the array and return the middle element. Since the majority element appears more than n/2 times, it will always occupy the middle position after sorting. |
| 41 | + |
| 42 | +**Time Complexity:** O(n log n) - due to sorting |
| 43 | +**Space Complexity:** O(1) or O(log n) depending on sorting implementation |
| 44 | + |
| 45 | +```java |
| 46 | +class Solution { |
| 47 | + public int majorityElement(int[] nums) { |
| 48 | + Arrays.sort(nums); |
| 49 | + return nums[nums.length / 2]; |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## Approach 3: HashMap Counting |
| 57 | + |
| 58 | +Use a HashMap to count occurrences of each element. Iterate through the array once to build the frequency map, then find the element with count greater than n/2. |
| 59 | + |
| 60 | +**Time Complexity:** O(n) - single pass to count frequencies |
| 61 | +**Space Complexity:** O(n) - hashmap stores up to n elements |
| 62 | + |
| 63 | +```java |
| 64 | +class Solution { |
| 65 | + public int majorityElement(int[] nums) { |
| 66 | + HashMap<Integer, Integer> map = new HashMap<>(); |
| 67 | + for (int ele : nums) { |
| 68 | + map.put(ele, map.getOrDefault(ele, 0) + 1); |
| 69 | + } |
| 70 | + int level = nums.length / 2; |
| 71 | + for (int key : map.keySet()) { |
| 72 | + if (map.get(key) > level) return key; |
| 73 | + } |
| 74 | + return -1; |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +## Approach 4: Boyer-Moore Voting Algorithm (Optimal) |
| 82 | + |
| 83 | +Use a voting algorithm that maintains a candidate element and a count. This algorithm leverages the fact that the majority element appears more than n/2 times: |
| 84 | +- If count is 0, set current element as new candidate |
| 85 | +- If current element matches candidate, increment count |
| 86 | +- If current element differs from candidate, decrement count |
| 87 | + |
| 88 | +The majority element will always be the final candidate. |
| 89 | + |
| 90 | +**Time Complexity:** O(n) - single pass through the array |
| 91 | +**Space Complexity:** O(1) - only using constant extra space |
| 92 | + |
| 93 | +```java |
| 94 | +class Solution { |
| 95 | + public int majorityElement(int[] nums) { |
| 96 | + int candidate = 0, count = 0; |
| 97 | + for (int num : nums) { |
| 98 | + if (count == 0) { |
| 99 | + candidate = num; |
| 100 | + } |
| 101 | + if (num == candidate) { |
| 102 | + count++; |
| 103 | + } else { |
| 104 | + count--; |
| 105 | + } |
| 106 | + } |
| 107 | + return candidate; |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## Dry Run Example |
| 115 | +Example Input: `nums = [3,2,3]` |
| 116 | + |
| 117 | +**Approach 1 (Brute Force):** |
| 118 | +- i=0, nums[0]=3, inner loop finds 2 occurrences of 3, 2 > 1, return 3 |
| 119 | +- i=1, nums[1]=2, inner loop finds 1 occurrence of 2, 1 not > 1 |
| 120 | + |
| 121 | +**Approach 2 (Sorting):** |
| 122 | +- Sorted array: [2,3,3], middle element is 3 |
| 123 | + |
| 124 | +**Approach 3 (HashMap):** |
| 125 | +- Map: {3:2, 2:1}, 3 appears 2 times > 1, return 3 |
| 126 | + |
| 127 | +**Approach 4 (Voting):** |
| 128 | +- i=0, num=3, count=0, set candidate=3, count=1 |
| 129 | +- i=1, num=2, count=1, 2 ≠ 3, count=0 |
| 130 | +- i=2, num=3, count=0, set candidate=3, count=1 |
| 131 | +- Final candidate = 3 |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +## Time and Space Complexity Summary |
| 136 | +- **Approach 1 (Brute Force):** O(n²) time, O(1) space |
| 137 | +- **Approach 2 (Sorting):** O(n log n) time, O(1)/O(log n) space |
| 138 | +- **Approach 3 (HashMap):** O(n) time, O(n) space |
| 139 | +- **Approach 4 (Voting Algorithm):** O(n) time, O(1) space |
| 140 | + |
| 141 | +**Recommendation:** Use Approach 4 (Boyer-Moore Voting Algorithm) as it's the most optimal solution with linear time and constant space complexity. |
0 commit comments