开发者

How to remove all proper subsets?

开发者 https://www.devze.com 2023-01-19 13:21 出处:网络
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?

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

0

精彩评论

暂无评论...
验证码 换一张
取 消