मध्यम 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 के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

अभी कोई पास हुआ समाधान नहीं है। इसे हल करें और पहला स्थान पाएँ।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।