forked from codehouseindia/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoreVoting.java
More file actions
60 lines (51 loc) · 1.34 KB
/
moreVoting.java
File metadata and controls
60 lines (51 loc) · 1.34 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
//Subscribed on YouTube by AMRITSARI KING. Don't have facebook id.
class MajorityElement
{
void printMajority(int a[], int size)
{
int cand = findCandidate(a, size);
if (isMajority(a, size, cand))
System.out.println(" " + cand + " ");
else
System.out.println("No Majority Element");
}
int findCandidate(int a[], int size)
{
int maj_index = 0, count = 1;
int i;
for (i = 1; i < size; i++)
{
if (a[maj_index] == a[i])
count++;
else
count--;
if (count == 0)
{
maj_index = i;
count = 1;
}
}
return a[maj_index];
}
=
boolean isMajority(int a[], int size, int cand)
{
int i, count = 0;
for (i = 0; i < size; i++)
{
if (a[i] == cand)
count++;
}
if (count > size / 2)
return true;
else
return false;
}
public static void main(String[] args)
{
MajorityElement majorelement = new MajorityElement();
int a[] = new int[]{1, 3, 3, 1, 2};
int size = a.length;
majorelement.printMajority(a, size);
}
}