Solving some anagrams can force the intrusive music out of your working memory, allowing it to be replaced with other more amenable thoughts, scientists said.
However, trying anything too difficult seemed to have little effect, The Telegraph reported.
"The key is to find something that will give the right level of challenge. If you are cognitively engaged, it limits the ability of intrusive songs to enter your head," said Dr Ira Hyman, a music psychologist at Western Washington University who conducted the research.
"Likewise, if you are trying something too hard, then your brain will not be engaged successfully, so that music can come back. You need to find that bit in the middle where there is not much space left in the brain. That will be different for each individual," Hyman said.
More From This Section
Hyman and his team conducted a series of tests on volunteers by playing them popular songs in an attempt to find out how tunes can become stuck in long term memory.
They tested whether performing puzzles such as Sudoku or anagrams would help to reduce the recurrence of the earworms.
They found that while Sudoku puzzles could help prevent the songs from replaying their heads, if they were too difficult it had little effect.
Anagrams were more successful and they found that solving those with five letters gave the best results.
The team found that Lady Gaga was the most common artist to get stuck in people's heads, with four of her catchy pop songs being the most likely to become earworms - Alejandro, Bad Romance, Just Dance and Paparazzi.