Skip to content

Commit cd6c364

Browse files
solves majority element in python
1 parent 4798e4d commit cd6c364

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/IntersectionOf2LinkedLists.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/intersecction_of_two_linked_lists.py) |
4949
| 167 | [Two Sum II - Input Array is Sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/TwoSumIIInputArrayIsSorted.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/two_sum_ii.py) |
5050
| 168 | [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ExcelSheetColumnTitle.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/excel_sheet_column_title.py) |
51-
| 169 | [Majority Element](https://leetcode.com/problems/majority-element) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MajorityElement.java) |
51+
| 169 | [Majority Element](https://leetcode.com/problems/majority-element) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MajorityElement.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/majority_element.py) |
5252
| 170 | [Two Sum III - Data Structure Design](https://leetcode.com/problems/two-sum-iii-data-structure-design) | Easy | |
5353
| 171 | [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ExcelSheetColumnNumber.java) |
5454
| 172 | [Factoring Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/FactorialTrailingZeros.java) |

python/majority_element.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def majorityElement(self, nums: List[int]) -> int:
6+
element, count = nums[0], 0
7+
for number in nums:
8+
count += 1 if number == element else -1
9+
if count == 0:
10+
element = number
11+
count = 1
12+
return element

0 commit comments

Comments
 (0)