I have some data that I read into a jagged array of strings. An example of such arrays:
string[] row = { "John", "Apple", "Orange", "Banana" };
and I will have an array to hold these rows of strings. A very long one.
What I want to do is find all the rows with the same name (“John“). Then compare these rows to see if they are identical. Print all of them if there are any exceptions, otherwise continue to next name.
So for example if I have 10 rows of “John“s, I want to check whether the attributes on all 10 of them are exactly “Apple“, “Orange” and “Banana“. And I will have thousands of names other than “John” that I want to do the same thing with.
Is there an efficient algorithm to do this? I will be writing it in C#.
Using Linq you could do the following:
However, this does require the creation of a custom comparer class as follows:
The results of using John would be 3 rows and the results with Sam would be 2 rows. If you want to ignore case you would just have to change
OrderBy(s => s)toOrderBy(s => s, StringComparer.OrdinalIgnoreCase)