[Solved] check if string contains same characters twice [closed]


If you want to check for any word to be used twice, use the Split function to make a string into words, and then Group to get counts:

string input = "MyString MyString";
var words = input.Split().GroupBy(s => s).ToDictionary(
                                                  g => g.Key, 
                                                  g => g.Count()
                                          );

The dictionary words will be a set of key and value pairs where the key is the word, and the value is the number of times its in your input string. If you want to find words which occur more than once:

bool hasDuplicateWords = words.Any(w => w.Value > 1);

To find which words occur multiple times:

var duplicateWords = words.Where(w => w.Value > 1);

Edit: After editting your question, it seems you are not parsing simple strings, but parsing XML code. You should use an XML parser to work with XML, something like this (not checked in editor):

var input = "<Item> MyString <Item> <Item> MyString <Item>";
var xml = XElement.Parse(input);

bool hasDuplicateWords = xml.Children
                            .GroupBy(x => x.Name)
                            .Any(x => x.Count() > 1);

2

solved check if string contains same characters twice [closed]