-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathvotingCount.java
75 lines (58 loc) · 1.56 KB
/
votingCount.java
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
import java.util.*;
public class votingCount {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String Candi[]=new String[100];
String vote[]=new String[100];
int invalidCount=0, flg=0;
// taking input
int N=sc.nextInt();
int Vcount[]=new int[N];
for(int Vc=0;Vc<N;Vc++) {
Vcount[Vc]=0;
}
for(int n=0; n<N ; n++) {
Candi[n]=sc.next().toUpperCase();
}
int V=sc.nextInt();
for(int v=0; v<V; v++) {
vote[v]=sc.next().toUpperCase();
flg=0;
for(int i=0;i<N;i++) {
if(vote[v].equals(Candi[i])){
Vcount[i] +=1;
flg=1;
// System.out.println(Vcount[i]);
}
}
if(flg==0) {
invalidCount +=1;
}
}
for(int Vc=0;Vc<N;Vc++) {
System.out.print(Candi[Vc]+"="+Vcount[Vc]+" ");
}
//sorting
for (int i = 0; i < Vcount.length; i++) {
// Inner nested loop pointing 1 index ahead
for (int j = i + 1; j < Vcount.length; j++) {
// Checking elements
int temp = 0;
String tempp=null;
if (Vcount[j] < Vcount[i]) {
// Swapping
temp = Vcount[i];
Vcount[i] = Vcount[j];
Vcount[j] = temp;
tempp = Candi[i];
Candi[i] = Candi[j];
Candi[j] = tempp;
}
}
}
System.out.println("invalid votes= "+invalidCount);
//winner
System.out.println("Winner "+Candi[N-1]);
}
}