|
| 1 | +--- |
| 2 | +id: majority-element |
| 3 | +title: Majority Element |
| 4 | +sidebar_label: Majority-Element |
| 5 | +tags: |
| 6 | + - Beginner |
| 7 | + - Geeks for Geeks |
| 8 | + - CPP |
| 9 | + - Python |
| 10 | + - DSA |
| 11 | +description: "This is a solution to the Majority Element Problem on Geeks for Geeks." |
| 12 | +--- |
| 13 | + |
| 14 | +This tutorial contains a complete walk-through of the Majority Element problem from the Geeks for Geeks website. It features the implementation of the solution code in two programming languages: Python and C++. |
| 15 | + |
| 16 | +## Problem Description |
| 17 | +Given an array `A` of `N` elements. Find the majority element in the array. A majority element in an array `A` of size `N` is an element that appears strictly more than `N/2` times in the array. |
| 18 | + |
| 19 | +## Examples |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | + |
| 23 | +``` |
| 24 | +Input: |
| 25 | +N = 3 |
| 26 | +A[] = {1,2,3} |
| 27 | +Output: |
| 28 | +-1 |
| 29 | +Explanation: |
| 30 | +Since, each element in |
| 31 | +{1,2,3} appears only once so there |
| 32 | +is no majority element. |
| 33 | +``` |
| 34 | + |
| 35 | +**Example 2:** |
| 36 | + |
| 37 | +``` |
| 38 | +Input: |
| 39 | +N = 5 |
| 40 | +A[] = {3,1,3,3,2} |
| 41 | +Output: |
| 42 | +3 |
| 43 | +Explanation: |
| 44 | +Since, 3 is present more |
| 45 | +than N/2 times, so it is |
| 46 | +the majority element. |
| 47 | +``` |
| 48 | + |
| 49 | +## Your Task |
| 50 | +The task is to complete the function majorityElement() which returns the majority element in the array. If no majority exists, return -1. |
| 51 | + |
| 52 | +Expected Time Complexity: $O(n)$ |
| 53 | +Expected Auxiliary Space: $O(1)$ |
| 54 | + |
| 55 | +## Constraints |
| 56 | + |
| 57 | +`100 <= N <= 2*10^9` |
| 58 | + |
| 59 | + |
| 60 | +## Code Implementation |
| 61 | + |
| 62 | +<Tabs> |
| 63 | + <TabItem value="Python" label="Python" default> |
| 64 | + <SolutionAuthor name="@ishiitamukherjee2004"/> |
| 65 | + ```py |
| 66 | + class Solution: |
| 67 | + |
| 68 | + |
| 69 | +def majority_element(A): |
| 70 | + count = 0 |
| 71 | + candidate = None |
| 72 | + for num in A: |
| 73 | + if count == 0: |
| 74 | + candidate = num |
| 75 | + count = 1 |
| 76 | + elif candidate == num: |
| 77 | + count += 1 |
| 78 | + else: |
| 79 | + count -= 1 |
| 80 | + return candidate |
| 81 | + |
| 82 | + ``` |
| 83 | + |
| 84 | + </TabItem> |
| 85 | + <TabItem value="C++" label="C++"> |
| 86 | + <SolutionAuthor name="@ishitamukherjee2004"/> |
| 87 | + |
| 88 | + ```cpp |
| 89 | + int majorityElement(vector<int>& A) { |
| 90 | + int count = 0; |
| 91 | + int candidate; |
| 92 | + for (int num : A) { |
| 93 | + if (count == 0) { |
| 94 | + candidate = num; |
| 95 | + count = 1; |
| 96 | + } else if (candidate == num) { |
| 97 | + count++; |
| 98 | + } else { |
| 99 | + count--; |
| 100 | + } |
| 101 | + } |
| 102 | + return candidate; |
| 103 | +} |
| 104 | + |
| 105 | +``` |
| 106 | + |
| 107 | + </TabItem> |
| 108 | + |
| 109 | + <TabItem value="Java" label="Java" default> |
| 110 | + <SolutionAuthor name="@ishiitamukherjee2004"/> |
| 111 | + ```java |
| 112 | + public int majorityElement(int[] A) { |
| 113 | + int count = 0; |
| 114 | + int candidate = 0; |
| 115 | + for (int num : A) { |
| 116 | + if (count == 0) { |
| 117 | + candidate = num; |
| 118 | + count = 1; |
| 119 | + } else if (candidate == num) { |
| 120 | + count++; |
| 121 | + } else { |
| 122 | + count--; |
| 123 | + } |
| 124 | + } |
| 125 | + return candidate; |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | + ``` |
| 130 | + |
| 131 | + </TabItem> |
| 132 | + |
| 133 | + <TabItem value="Javascript" label="Javascript" default> |
| 134 | + <SolutionAuthor name="@ishiitamukherjee2004"/> |
| 135 | + ```js |
| 136 | + |
| 137 | +function majorityElement(A) { |
| 138 | + let count = 0; |
| 139 | + let candidate; |
| 140 | + for (let num of A) { |
| 141 | + if (count === 0) { |
| 142 | + candidate = num; |
| 143 | + count = 1; |
| 144 | + } else if (candidate === num) { |
| 145 | + count++; |
| 146 | + } else { |
| 147 | + count--; |
| 148 | + } |
| 149 | + } |
| 150 | + return candidate; |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | + ``` |
| 155 | + |
| 156 | + </TabItem> |
| 157 | +</Tabs> |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | +## Time Complexity |
| 163 | + |
| 164 | +The time complexity is $O(n)$ because the operations involve a fixed number of steps regardless of the size of N: |
| 165 | + |
| 166 | +## Space Complexity |
| 167 | + |
| 168 | +The space complexity is $O(1)$ as well since the operations use a constant amount of extra space for storing the products and concatenated strings. |
0 commit comments