Skip to content

Instantly share code, notes, and snippets.

@csharpforevermore
Created October 10, 2018 15:59
Show Gist options
  • Save csharpforevermore/fda97b11807900a2d16f049df5c87ef7 to your computer and use it in GitHub Desktop.
Save csharpforevermore/fda97b11807900a2d16f049df5c87ef7 to your computer and use it in GitHub Desktop.
Compare two sets of data where I want to confirm ALL elements in one subset can be found in the other superset
var subset = new[] { 2, 4, 6, 8 };
var superset = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// (when comparing small sets, consider O(n*m) solution:)
public static bool ContainsAll<T>(this IEnumerable<T> source, IEnumerable<T> values)
{
bool contained = !subset.Except(superset).Any
return contained;
}
// (when comparing large sets, consider O(n+m) solution:)
public static bool ContainsAll<T>(this IEnumerable<T> source, IEnumerable<T> values)
{
HashSet<int> hashSet = new HashSet<int>(superset);
bool contained = subset.All(i => hashSet.Contains(i));
return contained;
}
// or
public static bool ContainsAll<T>(this IEnumerable<T> source, IEnumerable<T> values)
{
return values.All(value => source.Contains(value));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment