|
| 1 | +--- |
| 2 | +id: Majority-Element |
| 3 | +title: Majority Element |
| 4 | +sidebar_label: 0169-Majority-Element |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Hash Table |
| 8 | + - Sorting |
| 9 | + - Counting |
| 10 | +description: "Given an array nums of size n, return the majority element. |
| 11 | +
|
| 12 | +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." |
| 13 | +--- |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +## Problem Statement |
| 18 | + |
| 19 | +Given an array nums of size n, return the majority element. |
| 20 | + |
| 21 | +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. |
| 22 | + |
| 23 | +**Follow-up:** Could you solve the problem in linear time and in O(1) space? |
| 24 | + |
| 25 | + |
| 26 | +### Examples |
| 27 | + |
| 28 | +**Example 1:** |
| 29 | + |
| 30 | +```plaintext |
| 31 | +Input: nums = [3,2,3] |
| 32 | +Output: 3 |
| 33 | +``` |
| 34 | + |
| 35 | +**Example 2:** |
| 36 | + |
| 37 | +```plaintext |
| 38 | +Input: nums = [2,2,1,1,1,2,2] |
| 39 | +Output: 2 |
| 40 | +``` |
| 41 | + |
| 42 | +### Constraints |
| 43 | + |
| 44 | +- `n == nums.length` |
| 45 | +- `1 <= n <= 5 * 104` |
| 46 | +- `-109 <= nums[i] <= 109` |
| 47 | + |
| 48 | + |
| 49 | +## Solution |
| 50 | + |
| 51 | +If the array contains a majority element, its occurrence must be greater than the floor(N/2). |
| 52 | +Now, we can say that the count of minority elements and majority elements is equal up to a certain point in the array. |
| 53 | +So when we traverse through the array we try to keep track of the count of elements and the element itself for which we are tracking the count. |
| 54 | + |
| 55 | +After traversing the whole array, we will check the element stored in the variable. |
| 56 | +The question states that the array must contain a majority element, so the stored element will be that one |
| 57 | + |
| 58 | +### Approach |
| 59 | + |
| 60 | +#### Algorithm |
| 61 | + |
| 62 | +1. Initialize 2 variables: |
| 63 | + - Count – for tracking the count of element |
| 64 | + - Element – for which element we are counting |
| 65 | +2. Traverse through the given array. |
| 66 | + - If Count is 0 then store the current element of the array as Element. |
| 67 | + - If the current element and Element are the same increase the Count by 1. |
| 68 | + - If they are different decrease the Count by 1. |
| 69 | +3. The integer present in Element should be the result we are expecting |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +#### Solution |
| 74 | + |
| 75 | +### Java Solution |
| 76 | + |
| 77 | +```Java |
| 78 | +import java.util.*; |
| 79 | + |
| 80 | +public class tUf { |
| 81 | + public static int majorityElement(int []v) { |
| 82 | + //size of the given array: |
| 83 | + int n = v.length; |
| 84 | + int cnt = 0; // count |
| 85 | + int el = 0; // Element |
| 86 | + |
| 87 | + //applying the algorithm: |
| 88 | + for (int i = 0; i < n; i++) { |
| 89 | + if (cnt == 0) { |
| 90 | + cnt = 1; |
| 91 | + el = v[i]; |
| 92 | + } else if (el == v[i]) cnt++; |
| 93 | + else cnt--; |
| 94 | + } |
| 95 | + |
| 96 | + return el; |
| 97 | + } |
| 98 | + |
| 99 | + public static void main(String args[]) { |
| 100 | + int[] arr = {2, 2, 1, 1, 1, 2, 2}; |
| 101 | + int ans = majorityElement(arr); |
| 102 | + System.out.println("The majority element is: " + ans); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | +} |
| 107 | + |
| 108 | +``` |
| 109 | + |
| 110 | +### C++ Solution |
| 111 | + |
| 112 | +```cpp |
| 113 | +#include <bits/stdc++.h> |
| 114 | +using namespace std; |
| 115 | + |
| 116 | +int majorityElement(vector<int> v) { |
| 117 | + |
| 118 | + //size of the given array: |
| 119 | + int n = v.size(); |
| 120 | + int cnt = 0; // count |
| 121 | + int el; // Element |
| 122 | + |
| 123 | + //applying the algorithm: |
| 124 | + for (int i = 0; i < n; i++) { |
| 125 | + if (cnt == 0) { |
| 126 | + cnt = 1; |
| 127 | + el = v[i]; |
| 128 | + } |
| 129 | + else if (el == v[i]) cnt++; |
| 130 | + else cnt--; |
| 131 | + } |
| 132 | + |
| 133 | +return el; |
| 134 | +} |
| 135 | + |
| 136 | +int main() |
| 137 | +{ |
| 138 | + vector<int> arr = {2, 2, 1, 1, 1, 2, 2}; |
| 139 | + int ans = majorityElement(arr); |
| 140 | + cout << "The majority element is: " << ans << endl; |
| 141 | + return 0; |
| 142 | +} |
| 143 | +``` |
| 144 | +
|
| 145 | +### Python Solution |
| 146 | +
|
| 147 | +```python |
| 148 | +def majorityElement(arr): |
| 149 | + # Size of the given array |
| 150 | + n = len(arr) |
| 151 | + cnt = 0 # Count |
| 152 | + el = None # Element |
| 153 | +
|
| 154 | + # Applying the algorithm |
| 155 | + for i in range(n): |
| 156 | + if cnt == 0: |
| 157 | + cnt = 1 |
| 158 | + el = arr[i] |
| 159 | + elif el == arr[i]: |
| 160 | + cnt += 1 |
| 161 | + else: |
| 162 | + cnt -= 1 |
| 163 | +
|
| 164 | + return el |
| 165 | + |
| 166 | +
|
| 167 | +arr = [2, 2, 1, 1, 1, 2, 2] |
| 168 | +ans = majorityElement(arr) |
| 169 | +print("The majority element is:", ans) |
| 170 | +``` |
| 171 | + |
| 172 | +### Complexity Analysis |
| 173 | + |
| 174 | +- **Time complexity**: $O(N)$, where N = size of the given array. |
| 175 | +- **Space complexity**: $O(1)$ as we are not using any extra space. |
| 176 | + |
0 commit comments