Refactor loop in Anagram.