First of all, taken as arrays, row 0 and row 2 are not duplicates. They just have the same set of elements. However, if you just want to remove those kind of rows, you could do something like:
string[][] GetNonDuplicates(string[][] jagged)
{
//not a hashset, but a dictionary. A value of false means that the row
//is not duplicate, a value of true means that at least one dulicate was found
Dictionary<string[], bool> dict =
new Dictionary<string[], bool>(new RowEqualityComparer());
foreach(string[] row in jagged)
{
//if a duplicate is found - using the hash and the compare method
if (dict.ContainsKey(row))
{
dict[row] = true; //set value to true
}
else
{
dict.Add(row, false); //first time we see this row, add it
}
}
//just pop out all the keys which have a value of false
string[][] result = dict.Where(item => !item.Value)
.Select(item => item.Key)
.ToArray();
return result;
}
...
string[][] jagged = new []{new []{"A","B"} ,
new []{"C","D","E"},
new []{"B", "A"},
new []{"E","A"}};
string[][] nonDuplicates = GetNonDuplicates(jagged);
where RowEqualityComparer is:
class RowEqualityComparer : IEqualityComparer<string[]>
{
public bool Equals(string[] first, string[] second)
{
// different legths - different rows
if (first.Length != second.Length)
return false;
//we need to copy the arrays because Array.Sort
//will change the original rows
var flist = first.ToList();
flist.Sort();
var slist = second.ToList();
slist.Sort();
//loop and compare one by one
for (int i=0; i < flist.Count; i++)
{
if (flist[i]!=slist[i])
return false;
}
return true;
}
public int GetHashCode(string[] row)
{
//I have no idea what I'm doing, just some generic hash code calculation
if (row.Length == 0)
return 0;
int hash = row[0].GetHashCode();
for (int i = 1; i < row.Length; i++)
hash ^= row[i].GetHashCode();
return hash;
}
}