Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Guess the song title

I have this problem where I listen to so much music that I can never remember what songs are called. But even if I remember the lyrics, I won't always know the song's name. But lucky for me, there's a pretty neat formula to determine1 the title of a song just based on its lyrics.

I'll define the way a song title can be guessed as the line repeated in its lyrics the most times. If multiple lines are repeated the same number of times, choose the shortest of them. If multiple lyrics both occur the same number of times and are of equal length, you may choose any of them.

Given an input of a multiline string where each line is a lyric (you may take input as a list of lines), output the predicted title of the song, using the method outlined above.

For example, say this totally real song was stuck in my head and I couldn't remember its name:

Hello, world
Hello, world
I just got to say it, hello world
Goodbye, world
Goodbye, world
Goodbye

I can figure using this formula that the song's title is "Hello, world".

You may assume that the input string will contain no empty lines.

Test cases:

Links go to pastebins of the lyrics, with any " removed.


This is , so shortest code in bytes wins.

1 Accuracy not guaranteed. No refunds.

Answer*

Draft saved
Draft discarded
Cancel
2
  • 1
    \$\begingroup\$ Defeating the creator :) \$\endgroup\$ Commented Jan 9, 2023 at 5:52
  • \$\begingroup\$ Cool that Vyxal sort of has a builtin for this. Nice solution \$\endgroup\$ Commented Jan 9, 2023 at 15:37

AltStyle によって変換されたページ (->オリジナル) /