-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetMajorityElementInArray.cs
More file actions
162 lines (143 loc) · 5.05 KB
/
GetMajorityElementInArray.cs
File metadata and controls
162 lines (143 loc) · 5.05 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
namespace DA.Algorithms.Problems
{
public static class GetMajorityElementInArray
{
/// <summary>
/// Find the majority element, which appears more than (array.Length/2) times
/// <para>Time Complexity - O(n^2)</para>
/// </summary>
///
/// <returns>
/// Returns majority element if so exists, otherwise return int.MinValue
/// </returns>
public static int GetMajorityElement (int[] array)
{
int majorityElement = 0, count = 0, maxCount = 0;
for (int i = 0; i < array.Length; i++)
{
for (int j = i + 1; j < array.Length; j++)
{
if (array[i] == array[j])
{
++count;
}
}
if (count > maxCount)
{
majorityElement = array[i];
maxCount = count;
}
}
return (maxCount > (array.Length / 2)) ? majorityElement : int.MinValue;
}
/// <summary>
/// Find the majority element, which appears more than (array.Length/2) times using array sorting algorithm
/// <para>Time Complexity - O(n.logn)</para>
/// </summary>
///
/// <returns>
/// Returns majority element if so exists, otherwise return int.MinValue
/// </returns>
public static int GetMajorityElementUsingSorting (int[] array)
{
int count = 0;
int candidate = 0;
int majorityIndex = array.Length / 2;
Array.Sort (array);
candidate = array[majorityIndex];
for (int i = 0; i < array.Length; i++)
{
if (array[i] == candidate)
{
++count;
}
}
return (count > (array.Length / 2)) ? array[majorityIndex] : int.MinValue;
}
/// <summary>
/// Find the majority element, which appears more than (array.Length/2) times using Moore's Voting Algorithm
/// <para>Time Complexity - O(n)</para>
/// </summary>
///
/// <returns>
/// Returns majority element if so exists, otherwise return int.MinValue
/// </returns>
public static int GetMajorityElementMoores (int[] array)
{
int majorityIndex = 0;
int count = 1;
for (int i = 1; i < array.Length; i++)
{
if (array[majorityIndex] == array[i])
{
++count;
}
else
{
--count;
}
if (count == 0)
{
majorityIndex = i;
count = 1;
}
}
int candidate = array[majorityIndex];
count = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] == candidate)
{
++count;
}
}
return (count > (array.Length / 2)) ? array[majorityIndex] : int.MinValue;
}
/// <summary>
/// Find if the array has a majority element and find the majority element.
/// </summary>
public static bool IsMajority (int[] array)
{
int halfSize = array.Length / 2;
int majority = array[halfSize];
int firstIndex = FirstIndex (array, 0, array.Length - 1, majority);
bool isIndexInRange = (firstIndex + halfSize) <= (array.Length - 1);
bool isEqualToMajority = array[firstIndex + halfSize] == majority;
return isIndexInRange && isEqualToMajority;
}
/// <summary>
/// Find if the array has a majority element and find the majority element.
/// </summary>
public static bool IsMajority (int[] array, out int majorityElement)
{
int halfSize = array.Length / 2;
int majority = array[halfSize];
int firstIndex = FirstIndex (array, 0, array.Length - 1, majority);
bool isIndexInRange = (firstIndex + halfSize) <= (array.Length - 1);
bool isEqualToMajority = array[firstIndex + halfSize] == majority;
majorityElement = majority;
return isIndexInRange && isEqualToMajority;
}
public static int FirstIndex (int[] array, int value, int low, int high)
{
int middle = 0;
if (high >= low)
{
middle = (low + high) / 2;
}
if ((middle == 0 || array[middle - 1] < value) && array[middle] == value)
{
return middle;
}
else if (array[middle] < value)
{
return FirstIndex (array, value, middle + 1, high);
}
else
{
return FirstIndex (array, value, low, middle - 1);
}
}
}
}