Given a list of sets...
var sets = new List<HashSet<int>>(numTags);
How can I remove all the sets tha开发者_开发百科t are a proper subset of another?
Is this the best way to do it?
for (int i = 0; i < sets.Count; ++i)
{
for (int j = 0; j < sets.Count; ++j)
{
if (i != j && sets[i].IsProperSubsetOf(sets[j]))
{
sets.RemoveAt(i--);
}
}
}
I'm decrementing i
because I assume everything gets nudged down one after it gets removed so I have to check that slot again.
var toRemove = sets.Where(s => sets.Any(superset => s.IsProperSubsetOf(superset))).ToList();
foreach (var s in toRemove)
sets.Remove(s);
You don't need s != superset
check, cause no set is a proper subset of itself.
http://en.wikipedia.org/wiki/Proper_subset#proper_subset
精彩评论