Finding a List of Anagrams in C#
This is a very simple algorithm that I created for a game that I'm working on. The game is a word game for smaller children and one of the things that it needs to do is anagrams. So I had to come up with a function to help me find them:
public static Dictionary<string, System.Collections.Generic.List<string>> GetAnagramEquivalents(System.Collections.Generic.List<string> InputArray)
Dictionary<string, System.Collections.Generic.List<string>> ReturnList = new Dictionary<string, System.Collections.Generic.List<string>>();
for (int x = 0; x < InputArray.Count; ++x)
string InputString = new string(InputCharArray);
ReturnList.Add(InputString, new System.Collections.Generic.List<string>());
The code above is very basic. But the general idea is that with a set of words, if you sort the letters in alphabetical order you end up with an equivalent string. For instance both car and arc would be acr when sorted. Thus they are equivalent and anagrams of one another. So all you need to do is pass in a list of words and it passes back a dictionary of anagrams. That's all there is to it. So try it out, leave feedback, and happy coding.
Sign up for More Posts
I have no idea why you'd put yourself through that but if you'd like more posts from me then how about using my RSS feed? Take a look.