AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Program to find the election winner

It's election day. Write a program that counts votes cast and prints the person who won. Input: 1st line will contain the number of candidates. The next lines will contain 1 vote per line. The vote will be the serial number of the candidate. So for ten candidates, possible inputs are 1-10. Keep taking input till you get 0. Inputs are such that the highest vote-getter has unique number of votes. Output: 1st line will contain the serial number of the candidate who won. 2nd line will contain the number of votes that he/she got.

Sample input:

3

1

1

1

2

2

3

0

Sample output:

1

3

corejava x 352
programming x 168
Posted On : 2017-11-27 17:17:58.0
profile Divesh - anyforum.in Divesh
102180
up-rate
5
down-rate

Answers


If we assume an array of size total candidates and position of the element represents the candidate, We can make it even more short and simple. No need to use Map or List and multiple for loops. One loop is enough which is being used to take the inputs. Please refer the below answer.

import java.util.Scanner;

public class ElectionWinner {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n=input.nextInt();
int max = 0;
int winner=0;
int vote=0;
int temp=0;
int[] votes=new int[n];
while ((vote=input.nextInt())!=0){
temp=vote-1;
votes[temp]=votes[temp]+1;
temp= votes[temp];
if(max<temp){
max=temp;
winner=vote;
}
}
System.out.println("");
System.out.println(winner);
System.out.println(max);
}
}

Posted On : 2017-12-04 22:55:28
Satisfied : 3 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188221600
Reply This Thread
up-rate
5
down-rate

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


public class ElectionWinner {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n=input.nextInt();
int max = 0;
int winner=0;
int vote=0;
int temp=0;
Map<Integer, Integer> votes = new HashMap<Integer, Integer>(n);
while ((vote=input.nextInt())!=0){
if(votes.containsKey(vote))
votes.put(vote, votes.get(vote)+1);
else
votes.put(vote, 1);
temp= votes.get(vote);
if(max<temp){
max=temp;
winner=vote;
}
}
System.out.println("");
System.out.println(winner);
System.out.println(max);
}
}

Posted On : 2017-12-03 15:53:15
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188221600
Reply This Thread
up-rate
3
down-rate

import java.util.Scanner;
import java.util.*;

public class VoteCount{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
int voter=sc.nextInt();
int votercount[]=new int[voter+1];
List li=new ArrayList();
int i=0,count=0,j,k;
int vote=sc.nextInt();
while(vote!=0){
li.add(vote);
vote=sc.nextInt();

}
for(j=0;j<li.size();j++){
votercount[(int)li.get(j)]++;
}
int maxwinner=votercount[0];
int win=0;
for(i=0;i<votercount.length;i++){
if(votercount[i]>maxwinner){
maxwinner=votercount[i];
win=i;
}
}
System.out.println(win);
System.out.println(maxwinner);
}
}

Posted On : 2017-12-04 17:04:49
Satisfied : 1 Yes  0 No
profile Divesh - anyforum.in Divesh
102180
Reply This Thread
up-rate
2
down-rate
Comments
Hi Divesh,
If we assume an array of size total candidates and position of the element represents the candidate, We can make it even more short and simple. No need to use Map or List and multiple for loops. One loop is enough which is being used to take the inputs. Please refer the below answer.
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1882  21600
Posted On :2017-12-04 22:57:59.0
Leave a Comment

import java.util.Scanner;
public class T2
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n=input.nextInt();
int max = 0,winner=0,vote=0;
int[] votes=new int[n];
while ((vote=input.nextInt())!=0)
{
votes[vote-1]=votes[vote-1]+1;
}
for(int i=0;i<n;i++)
{
if(max<votes[i])
{
max=votes[i];
winner=i;
}
}
System.out.println("");
System.out.println(winner+1);
System.out.println(max);
}
}

Posted On : 2018-06-06 10:34:50
Satisfied : 2 Yes  0 No
profile Abhishek Kumar - anyforum.in Abhishek Kumar
040
Reply This Thread
up-rate
4
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in