- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find winner of an election where votes are represented as candidate names in C++
In this tutorial, we are going to write a program that finds the election winner. We will have an array of votes that each candidate got in the election. Let's see an example.
Input
{"A", "B", "C", "B", "A", "C", "D", "D", "A", "B", "D", "B", "A", "C", "D"}
Output
A
Here, A and B got the same number of votes. In that case, we have to select the winner based on the alphabetical order of their names.
Let's see the steps to solve the problem.
Initialize an array of string with dummy data.
Initialize a map with string as key and int as value.
Iterate over the votes array and count the each member votes. Use the map to store the votes count.
We have votes count with us. To find the election winner, iterate over the map and find the key with max votes.
If two members got the same votes, then check for their names.
Print the winner.
Example
Let's see the code.
#include "bits/stdc++.h" using namespace std; void findElectionWinner(string votes[], int total_votes) { map<string, int> candidate_votes_count; // counting each person votes for (int i = 0; i < total_votes; i++) { candidate_votes_count[votes[i]]++; } // finding winner int max_votes = 0; string election_winner; for (auto& entry : candidate_votes_count) { string key = entry.first; int val = entry.second; // checking the votes with max votes if (val > max_votes) { // updating max votes and member max_votes = val; election_winner = key; // comparing the name if the votes are equal } else if (val == max_votes && election_winner > key) { election_winner = key; } } cout << election_winner << endl; } int main() { string votes[] = {"A", "B", "C", "B", "A", "C", "D", "D", "A", "B", "D", "B", "A"}; findElectionWinner(votes, 13); return 0; }
Output
If you execute the above program, then you will get the following result.
A
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- In an election, the successful candidate registered ( 5,77,500 ) votes and his nearest rival secured 3,48,700 votes. By what margin did the successful candidate win the election?
- In an election, there were only two candidates. The winner polled 53% votes and won by 9600 votes. Find the total number of votes polled
- Find the winner of a game where scores are given as a binary string in Python
- Sum of two numbers where one number is represented as array of digits in C++
- Online Election in C++
- Why are numbers represented as objects in python?
- C++ code to find winner of math contest
- C++ program to find winner of card game
- Multiply Large Numbers represented as Strings in C++
- Adding one to number represented as array of digits in C++?
- C++ Program to find winner of unique bidding game
- C++ program to find winner of cell coloring game
- C++ program to find winner of ball removal game
- C++ code to find different winner and non-winner counts on a contest
- Evaluate a boolean expression represented as string in C++
