What is the most efficient way to write a method that will compare n lists and return all values ββthat are not displayed in all lists, so
var lists = new List<List<int>> { new List<int> { 1, 2, 3, 4 }, new List<int> { 2, 3, 4, 5, 8 }, new List<int> { 2, 3, 4, 5, 9, 9 }, new List<int> { 2, 3, 3, 4, 9, 10 } }; public IEnumerable<T> GetNonShared(this IEnumerable<IEnumerable<T>> lists) { //...fast algorithm here }
so that
lists.GetNonShared ();
returns 1, 5, 8, 9, 10
I had
public IEnumerable<T> GetNonShared(this IEnumerable<IEnumerable<T>> lists) { return list.SelectMany(item => item) .Except(lists.Aggregate((a, b) => a.Intersect(b)); }
But I was not sure that it was effective. The order does not matter. Thanks!
c # linq array-algorithms
Brad urani
source share