|
1 | 1 | ---
|
2 |
| -id: Boyer-Moore-Majority-voting-algorithm |
| 2 | +id: boyer-moore-majority-voting-algorithm |
3 | 3 | title: Boyer Moore Majority voting algorithm
|
4 | 4 | sidebar_label: Boyer Majority voting algorithm
|
5 | 5 | tags: [python, java, c++, algorithms, array, counting]
|
@@ -139,7 +139,7 @@ def majorityElement(v: List[int]) -> List[int]:
|
139 | 139 | Use a better data structure to reduce the number of look-up operations and hence the time complexity. Moreover, we have been calculating the count of the same element again and again – so we have to reduce that also.
|
140 | 140 |
|
141 | 141 | #### Approach
|
142 |
| - - Use a hashmap and store the elements as <key, value> |
| 142 | + - Use a hashmap and store the elements as `<key, value> ` |
143 | 143 | pairs. (Can also use frequency array based on the size of nums).
|
144 | 144 | - Here the key will be the element of the array and the
|
145 | 145 | value will be the number of times it occurs.
|
@@ -225,7 +225,7 @@ def majorityElement(arr):
|
225 | 225 |
|
226 | 226 | ## Optimal Approach (Extended Boyer Moore’s Voting Algorithm):
|
227 | 227 | #### Intuition
|
228 |
| -If the array contains the majority of elements, their occurrence must be greater than the floor(N/3). Now, we can say that the count of minority elements and majority elements is equal up to a certain point in the array. So when we traverse through the array we try to keep track of the counts of elements and the elements themselves for which we are tracking the counts. |
| 228 | +If the array contains the majority of elements, their occurrence must be greater than the `floor(N/3)`. Now, we can say that the count of minority elements and majority elements is equal up to a certain point in the array. So when we traverse through the array we try to keep track of the counts of elements and the elements themselves for which we are tracking the counts. |
229 | 229 |
|
230 | 230 | After traversing the whole array, we will check the elements stored in the variables. Then we need to check if the stored elements are the majority elements or not by manually checking their counts.
|
231 | 231 |
|
|
0 commit comments