Editor

Most Friends

#37 · Graphs · Beginner · 35 XP · about 6 min

A social app stores friendships as pairs of names: ['ana', 'ben'] means Ana and Ben are friends with each other. Write most_friends(pairs) that returns the name of the person who has the most friends.

If several people share the highest number of friends, return the name that comes first in alphabetical order.

Examples

most_friends([['ana', 'ben'], ['ben', 'cy'], ['ben', 'dee']])  # returns 'ben'
most_friends([['zoe', 'max'], ['max', 'lia'], ['lia', 'zoe']])  # returns 'lia'

In the second example everyone has two friends, so the tie goes to 'lia', the first of the three names alphabetically.

Note

  • There is at least one pair. Nobody is paired with themselves and no pair appears twice.
  • Names are lowercase.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.