score:3
var data = new [] {
new List<int> {1, 2, 3, 4, 5},
new List<int> {6, 7, 8, 9, 1},
new List<int> {3, 6, 9, 2, 0, 1},
new List<int> {1, 2, 9, 0, 5},
new List<int> {1, 7, 8, 6, 5, 4},
new List<int> {1},
new List<int> {},
null
};
IEnumerable<int> temp = null;
foreach (var arr in data)
if (arr != null && arr.Count != 0)
temp = temp == null ? arr : arr.Intersect(temp);
score:2
One way is to use a HashSet
. You can put the items of the first collection in the hash, then iterate each collection after the first and create an new hash that you add items from the current collection to if it's in the hash. At the end you assign that common hash set to the overall one and break if it's every empty. At the end you just return the overall hash set.
public IEnumerable<T> CommonItems<T>(IEnumerable<IEnumerable<T>> collections)
{
if(collections == null)
throw new ArgumentNullException(nameof(collections));
using(var enumerator = collections.GetEnumerator())
{
if(!enumerator.MoveNext())
return Enumerable<T>.Empty();
var overall = new HashSet<T>(enumerator.Current);
while(enumerator.MoveNext())
{
var common = new HashSet<T>();
foreach(var item in enumerator.Current)
{
if(hash.Contains(item))
common.Add(item);
}
overall = common;
if(overall.Count == 0)
break;
}
return overall;
}
}
score:6
You can chain Intersect
:
List<int> List1 = new List<int> {1, 2, 3, 4, 5};
List<int> List2 = new List<int> { 6, 7, 8, 9, 1 };
List<int> List3 = new List<int> { 3, 6, 9, 2, 0, 1 };
List<int> List4 = new List<int> { 1, 2, 9, 0, 5 };
List<int> List5 = new List<int> { 1, 7, 8, 6, 5, 4 };
List<int> List6 = new List<int> { 1 };
List<int> common = List1
.Intersect(List2)
.Intersect(List3)
.Intersect(List4)
.Intersect(List5)
.Intersect(List6)
.ToList();
score:6
var data = new List<List<int>> {
new List<int> {1, 2, 3, 4, 5},
new List<int> {6, 7, 2, 8, 9, 1},
new List<int> {3, 6, 9, 2, 0, 1},
new List<int> {1, 2, 9, 0, 5},
new List<int> {1, 7, 8, 6, 2, 5, 4},
new List<int> {1, 7, 2}
};
List<int> res = data
.Aggregate<IEnumerable<int>>((a, b) => a.Intersect(b))
.ToList();
The type of Aggregate is explicitly given, otherwise aggregation of two Lists would have to be List too. It can be easily adapted to run in parallel:
List<int> res = data
.AsParallel<IEnumerable<int>>()
.Aggregate((a, b) => a.Intersect(b))
.ToList();
EDIT
Except... it does not run in parallel. The problem is operations on IEnumerable are deferred, so even if they are logically merged in parallel context, the actual merging occurs in the ToList()
, which is single threaded. For parallel execution it would be better to leave IEnumerable and return to the Lists:
List<int> res = data
.AsParallel()
.Aggregate((a, b) => a.Intersect(b).ToList());
Source: stackoverflow.com
Related Query
- Find common items in multiple lists in C# linq
- Use LINQ to find complex combinations of items in two lists
- linq - return common items from n number of lists
- Find common items in list of lists of strings
- LINQ Query to Filter Items By Criteria From Multiple Lists
- LINQ How to return common objects across multiple lists
- Find all items from two different custom lists using LINQ (C#)
- Select items from multiple lists via linq
- Find duplicates within multiple lists using linq
- LINQ query to find items where multiple substrings are searched on one column
- Linq query to select only common items in two lists
- linq - find item in list within multiple lists
- LINQ / C# Remove all items from a list that are in multiple other lists
- Use linq to find items that appear in two lists
- Find common items in list of lists of user type
- find elements of different types in two lists with common property value, linq variant of two for each loops
- LINQ query to find if items in a list are contained in another list
- LINQ - Find all items in one list that aren't in another list
- Using LINQ to find duplicates across multiple properties
- Using LINQ to Objects to find items in one collection that do not match another
- LINQ Single() Exception for 0 or multiple items
- Get different and common items in two arrays with LINQ
- Linq find differences in two lists
- Merge multiple Lists into one List with LINQ
- Compare two lists to search common items
- How do I remove items from generic list, based on multiple conditions and using linq
- LINQ Comparing Two Lists - Add new, remove old, leave the ones in common
- How to use LINQ to find all combinations of n items from a set of numbers?
- Whats the 'modern' way to find common items in two Lists<T> of objects?
- Does this LINQ code perform multiple lookups on the original data?
More Query from same tag
- LINQ is select new necessary for efficiency
- Cannot implicitly convert type system linq IQueryable to system collections generic List
- Update field IEnumerable regarding duplicates c# EF
- Ensure IQueryable<T>.Where() runs a SQL query rather than filtering in memory
- Generating an array of integers with distinct numbers and the first one cannot be zero
- Linq query equivalent to sql
- EF LInq Left outer join sorted taking fist
- MVC Ajax form submission failed
- How can i get all subnode values separately from XML Response using LINQ to XML?
- How to flatten a GroupBy and select in linq
- Get xml data from external website
- How to make a Linq XML join With attributes on different elements
- Checklist Box Selected Items from LinQ
- DropDown datasource
- Nested foreach using by linq in asp.net mvc
- Map query result to typed list
- Linq: select grouped lists
- .NET: LINQ's Last()
- Creating, combining and caching lambda expressions
- XML Document to IEnumerable<XElement> and get values
- Linq expressions where empty string acts as wildcard
- Entity Framework inline SQL dynamically select table name
- Dynamic LINQ: Comparing Nested Data With Parent Property
- counting in linq on join
- Excluding specific records using LINQ query
- LINQ filter string with value string
- LINQ Query to retrieve items
- Adding N conditions to a LINQ query?
- LINQ to XML Query not working as desired
- Calculating totals in linq query