Created
October 10, 2018 15:59
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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