class Anagram { List findAnagrams(String subject, List candidates) { Map subjectCharsMap = countCharacters(subject); List matchTracker = new List(); for (String possible in candidates) { if (possible.toLowerCase() != subject.toLowerCase()) { Map possibleCharMap = countCharacters(possible); if (mapsMatch(subjectCharsMap, possibleCharMap)) { matchTracker.add(possible); } possibleCharMap.clear(); } } return matchTracker; } Map countCharacters(String word) { Map charTracker = new Map(); for (int counter = 0; counter < word.length; counter++) { var key = word[counter].toLowerCase(); charTracker[key] = charTracker.containsKey(key) ? charTracker[key] + 1 : 1; } return charTracker; } } bool mapsMatch(Map subjectCharsMap, Map possibleCharMap) { List trackingMatches = new List(); for (String key in possibleCharMap.keys) { if (subjectCharsMap.containsKey(key)) { trackingMatches.add(subjectCharsMap[key] == possibleCharMap[key]); } else { return false; } } for (bool result in trackingMatches) { if (result == false) { return false; } } return trackingMatches.length == subjectCharsMap.length; }