forked from YearOfProgramming/2017Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Python] Challenge 3 (Unreviewed) (YearOfProgramming#357)
* challenge 3 in python * Challenge 3 in python YearOfProgramming#2 (Whoops)
- Loading branch information
Showing
2 changed files
with
17 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Majority Element | ||
Premise | ||
|
||
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. | ||
|
||
You may assume that the array is non-empty and the majority element always exist in the array. | ||
|
||
for example, given array = [2,2,3,7,5,7,7,7,4,7,2,7,4,5,6,7,7,8,6,7,7,8,10,12,29,30,19,10,7,7,7,7,7,7,7,7,7] your program should return 7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def majority(array0): | ||
store = {} | ||
for i in array0: | ||
store[i] = store.get(i,0) + 1 | ||
|
||
for i in store.keys(): | ||
if store[i] > len(array0)//2: | ||
return i | ||
print('No majority found') |