Todos os desafios
Médio 0 resolvidos 0% de aceitação

Plurality Election

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

O que você vai aprender

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

Problema

Ver tradução

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.

Restrições

1 ≤ length of candidates ≤ 20

0 ≤ length of votes ≤ 1000

Names have 1 to 20 ASCII letters

Exemplos

Exemplo 1
Entrada:
candidates = ["Ava", "Ben", "Cruz"]
votes = ["Ben", "Ava", "Ben", "Cruz", "Ben"]

Saída esperada:
expected = ["Ben"]
Entrada:
std::vector<std::string> candidates = {"Ava", "Ben", "Cruz"};
std::vector<std::string> votes = {"Ben", "Ava", "Ben", "Cruz", "Ben"};

Saída esperada:
std::vector<std::string> expected = {"Ben"};
Entrada:
string[] candidates = new string[] {"Ava", "Ben", "Cruz"};
string[] votes = new string[] {"Ben", "Ava", "Ben", "Cruz", "Ben"};

Saída esperada:
string[] expected = new string[] {"Ben"};

Explicação

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

Exemplo 2
Entrada:
candidates = ["Ava", "Ben", "Cruz"]
votes = ["Cruz", "Ava", "ava", "Dana"]

Saída esperada:
expected = ["Ava", "Cruz"]
Entrada:
std::vector<std::string> candidates = {"Ava", "Ben", "Cruz"};
std::vector<std::string> votes = {"Cruz", "Ava", "ava", "Dana"};

Saída esperada:
std::vector<std::string> expected = {"Ava", "Cruz"};
Entrada:
string[] candidates = new string[] {"Ava", "Ben", "Cruz"};
string[] votes = new string[] {"Cruz", "Ava", "ava", "Dana"};

Saída esperada:
string[] expected = new string[] {"Ava", "Cruz"};

Explicação

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

Como abordar

Tente sozinho primeiro e depois abra uma dica de cada vez.

Dica 1

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

Dica 2

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

Dica 3

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

Complexidade esperada

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

Referências

Este desafio foi adaptado para o CodeUtility. Siga o enunciado e as restrições acima.

A melhor solução aprovada de cada programador. As mais rápidas, leves e mais bem avaliadas ficam no topo.

Ainda não há soluções aprovadas. Resolva e fique com o primeiro lugar.

Aprenda com outros

Soluções da comunidade

Ainda não há soluções publicadas. Resolva este desafio para compartilhar a primeira.