Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions data_structures/arrays/majority_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
PROBLEM : Find majority element in the given array.

PROBLEM DESCRIPTION :

Check failure on line 4 in data_structures/arrays/majority_element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/arrays/majority_element.py:4:22: W291 Trailing whitespace
Given a array of elements of size n.
The majority element is the element that appears more than [n/2] times.
We assume that the majority element is always exists in the array.

EXAMPLE :

Check failure on line 9 in data_structures/arrays/majority_element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/arrays/majority_element.py:9:10: W291 Trailing whitespace
input : array = [1,3,5,1,1]
output : 1
explanation : 1 appears three times in array which is greater than [n/2] i.e [5/2] = 2.

Check failure on line 12 in data_structures/arrays/majority_element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/majority_element.py:12:89: E501 Line too long (91 > 88)

APPROACH:
- In an array of elements of size n there exists only one element that appears greater than [n/2] times.

Check failure on line 15 in data_structures/arrays/majority_element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/majority_element.py:15:89: E501 Line too long (104 > 88)

- If we sort the given elements in the array the [n/2] element in the array must be the majority element.

Check failure on line 17 in data_structures/arrays/majority_element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/majority_element.py:17:89: E501 Line too long (105 > 88)
if we sort [1,3,5,1,1] it will be [1,1,1,3,5]

Check failure on line 18 in data_structures/arrays/majority_element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/arrays/majority_element.py:18:50: W291 Trailing whitespace
element present in the [n/2] index of the sorted array is majority element i.e 1 where n is size of array.

Check failure on line 19 in data_structures/arrays/majority_element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/majority_element.py:19:89: E501 Line too long (110 > 88)
"""


#function to find majority element
def majority_element(array:list)->int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/majority_element.py, please provide doctest for the function majority_element

'''
Return majority element in the list.
'''
array.sort()
return array[len(array)//2]


if __name__ == "__main__":
import doctest
doctest.testmod()

Check failure on line 34 in data_structures/arrays/majority_element.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

data_structures/arrays/majority_element.py:34:22: W292 No newline at end of file
Loading