logo CodeStepByStep logo

GetMajorityLastName

Language/Type: C# Dictionary collections
Related Links:

Write a method named GetMajorityLastName that accepts as its parameter a dictionary from strings to strings; the keys of the dictionary represent first names and the values represent last names. If there is a single common last name that is present in more than half of the key/value pairs in the dictionary passed in (a "majority" last name), your method should return that last name. If there is no majority last name, your method should return the string "?" .

For example, if the dictionary contains the following key/value pairs, the majority last name is "Smith" because it occurs 5 times, which is more than half of the nine pairs in the dictionary. Therefore your method would return "Smith".

{{"Hal", "Perkins"}, {"Mark", "Smith"}, {"Mike", "Smith"}, {"Stuart", "Reges"}, {"David", "Smith"}, {"Jean", "Reges"}, {"Geneva", "Smith"}, {"Amie", "Smith"}, {"Bruce", "Reges"}}

The following dictionaries don't have any majority last name because no last name occurs strictly greater than half the time. Therefore when passed either of the dictionaries below, your method would return "?" .

{{"Marty", "Stepp"}, {"Mehran", "Sahami"}, {"Keith", "Schwarz"}, {"Cynthia", "Lee"}, {"Yogurt", "Schwarz"}}
{{"Tywin", "Lannister"}, {"Rob", "Stark"}, {"Sansa", "Stark"}, {"Tyrion", "Lannister"}}

If the dictionary that contains only one key/value pair, that pair's value is the majority last name. An empty dictionary does not have any majority last name.

Constraints: You may declare at most one auxiliary data structure to help you solve this problem. Do not modify the dictionary that is passed in to your method as a parameter.

Method: Write a C# method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.