GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — coding-problems/Arrays/majority_element.py at master · Ankit-Mehra/coding-problems

Expand file tree

/

majority_element.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
94 lines (72 loc) · 1.98 KB
/

majority_element.py

Copy path

File metadata and controls

94 lines (72 loc) · 1.98 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'''
Majority Element
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.
Input: [3, 2, 3]
Output: 3
Input: [2, 2, 1, 1, 1, 2, 2]
Output: 2
=========================================
Sort the array and the result will the middle element.
Time Complexity: O(N LogN)
Space Complexity: O(1)
Use dictionary (hash map) and count the occurrences.
The result will be the one with more than ⌊ n/2 ⌋ occurrences.
Time Complexity: O(N)
Space Complexity: O(N)
Using Boyer-Moore voting algorithm. Choose a potential majority element and for each occurence add +1, but
if the current element isn't same substract -1.
When the counter is 0, the next element becomes the new potential majority element.
Time Complexity: O(N)
Space Complexity: O(1)
'''
##############
# Solution 1 #
##############
def majority_element_1(nums):
nums.sort()
return nums[len(nums) // 2]
##############
# Solution 2 #
##############
def majority_element_2(nums):
counter = {}
for num in nums:
if num in counter:
counter[num] += 1
else:
counter[num] = 1
half = len(nums) // 2
for num in counter:
if counter[num] > half:
return num
##############
# Solution 3 #
##############
def majority_element_3(nums):
majority = 0
count = 0
for num in nums:
if count == 0:
majority = num
if num == majority:
count += 1
else:
count -= 1
return majority
###########
# Testing #
###########
# Test 1
# Correct result => 3
arr = [3, 2, 3]
print(majority_element_1(arr))
print(majority_element_2(arr))
print(majority_element_3(arr))
# Test 2
# Correct result => 2
arr = [2, 2, 1, 1, 1, 2, 2]
print(majority_element_1(arr))
print(majority_element_2(arr))
print(majority_element_3(arr))
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.