-
Notifications
You must be signed in to change notification settings - Fork 0
/
MajorityElement.cpp
61 lines (56 loc) · 921 Bytes
/
MajorityElement.cpp
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
// // Brute Force TC O(N^2) SC (1)
int findMajorityElement(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
int maxCount = 0;
for (int j = 0; j < n; j++)
{
if (arr[i] == arr[j])
maxCount++;
}
if (maxCount > n / 2)
return arr[i];
}
return -1;
}
// Optimized using Hashmap TC O(N) SC O(N)
int findMajorityElement(int arr[], int n)
{
unordered_map<int, int> mp;
for (int i = 0; i < n; i++)
{
mp[arr[i]]++;
if (mp[arr[i]] > n / 2)
return arr[i];
}
return -1;
}
// Most Optimized Boyer-Moore majority vote algo TC O(N) SC O(1)
int findMajorityElement(int arr[], int n)
{
int ele = arr[0], count = 1;
for (int i = 1; i < n; i++)
{
if (arr[i] == ele)
count++;
else
count--;
if (count == 0)
{
ele = arr[i];
count = 1;
}
}
count = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] == ele)
{
count++;
}
}
if (count > n / 2)
return ele;
return -1;
}