Средне Решено: 0 Процент решений: 0%

Plurality Election

Count one vote per voter and return every candidate with the most votes.

Чему вы научитесь

  • Count votes per candidate
  • Ignore invalid votes
  • Return every candidate tied for the top count

Условие

Показать перевод

The student council election is a simple plurality vote: each voter writes one candidate's name, and the candidate with the most votes wins. A vote for a name that is not on the candidate list (a typo or a write-in) is thrown out. If several candidates share the highest count, they all win and share the office.

Implement plurality_election(candidates, votes).

Input

candidates: The candidates' names, all different.

votes: One name per voter. Names must match exactly, including case.

Output

Return the names of all winners in the order they appear in candidates. If no valid votes were cast, every candidate is tied at 0 and all are returned.

Ограничения

1 ≤ length of candidates ≤ 20

0 ≤ length of votes ≤ 1000

Names have 1 to 20 ASCII letters

Примеры

Пример 1
Ввод:
candidates = ["Ava", "Ben", "Cruz"]
votes = ["Ben", "Ava", "Ben", "Cruz", "Ben"]

Ожидаемый вывод:
expected = ["Ben"]
Ввод:
std::vector<std::string> candidates = {"Ava", "Ben", "Cruz"};
std::vector<std::string> votes = {"Ben", "Ava", "Ben", "Cruz", "Ben"};

Ожидаемый вывод:
std::vector<std::string> expected = {"Ben"};
Ввод:
string[] candidates = new string[] {"Ava", "Ben", "Cruz"};
string[] votes = new string[] {"Ben", "Ava", "Ben", "Cruz", "Ben"};

Ожидаемый вывод:
string[] expected = new string[] {"Ben"};

Пояснение

Ben has 3 votes, more than Ava and Cruz with 1 each, so ["Ben"].

Пример 2
Ввод:
candidates = ["Ava", "Ben", "Cruz"]
votes = ["Cruz", "Ava", "ava", "Dana"]

Ожидаемый вывод:
expected = ["Ava", "Cruz"]
Ввод:
std::vector<std::string> candidates = {"Ava", "Ben", "Cruz"};
std::vector<std::string> votes = {"Cruz", "Ava", "ava", "Dana"};

Ожидаемый вывод:
std::vector<std::string> expected = {"Ava", "Cruz"};
Ввод:
string[] candidates = new string[] {"Ava", "Ben", "Cruz"};
string[] votes = new string[] {"Cruz", "Ava", "ava", "Dana"};

Ожидаемый вывод:
string[] expected = new string[] {"Ava", "Cruz"};

Пояснение

ava and Dana are thrown out, leaving Ava and Cruz tied at 1: ["Ava", "Cruz"].

Как подступиться

Сначала попробуйте сами, затем открывайте подсказки по одной.

Подсказка 1

Start every candidate at 0 votes so candidates without votes still count.

Подсказка 2

Only count a vote when the name is exactly a candidate.

Подсказка 3

Find the highest count first, then collect everyone with that count in list order.

Ожидаемая сложность

O(c + v) time, O(c) extra space

Ссылки

Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.

Лучшее принятое решение каждого участника. Более быстрые, экономные и высоко оценённые решения стоят выше.

Принятых решений пока нет. Решите задачу и займите первое место.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.