Skip to content

Commit 9d885c1

Browse files
authored
Update majority_element.py
solved errors
1 parent 739f263 commit 9d885c1

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed
+9-11
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
"""
22
PROBLEM : Find majority element in the given array.
3-
43
PROBLEM DESCRIPTION :
54
Given a array of elements of size n.
65
The majority element is the element that appears more than [n/2] times.
76
We assume that the majority element is always exists in the array.
8-
97
EXAMPLE :
108
input : array = [1,3,5,1,1]
119
output : 1
12-
explanation : 1 appears three times in array which is greater than [n/2] i.e [5/2] = 2.
13-
10+
explanation : 1 appears three times in array,
11+
which is greater than [n/2] i.e [5/2] = 2.
1412
APPROACH:
15-
- In an array of elements of size n there exists only one element that appears greater than [n/2] times.
16-
17-
- If we sort the given elements in the array the [n/2] element in the array must be the majority element.
18-
if we sort [1,3,5,1,1] it will be [1,1,1,3,5]
19-
element present in the [n/2] index of the sorted array is majority element i.e 1 where n is size of array.
13+
- In an array of elements of size n,
14+
there exists only one element that appears greater than [n/2] times.
15+
- If we sort the given elements in the array the [n/2]th
16+
element in the array must be the majority element.
17+
- if we sort [1,3,5,1,1] it will be [1,1,1,3,5]
18+
element present in the [n/2] index of the sorted array
19+
is majority element i.e 1 where n is size of array.
2020
"""
2121

22-
2322
# function to find majority element
2423
def majority_element(array: list) -> int:
2524
"""
@@ -31,5 +30,4 @@ def majority_element(array: list) -> int:
3130

3231
if __name__ == "__main__":
3332
import doctest
34-
3533
doctest.testmod()

0 commit comments

Comments
 (0)