This question already has answers here:
Get the symmetric difference from generic lists
(7 answers)
The opposite of Intersect()
(8 answers)
Closed 9 years ago.
If I have two list and I want the elements that are common in both lists, I can use this code:
var listC = listA.Intersect(listB);
However, If I want the elements that are not common? And without duplicates? is possible with intersect?
Thanks.
Neither answer so far will include items from listB that aren't in listA. To get any item that is in either list, but is not in both lists:
listA.Union(listB).Except(listA.Intersect(listB));
Yep, that's possible. It's called Enumerable.Except.
Use this:
var result = listA.Except(listB); //maybe a .ToList() at the end,
//or passing an IEqualityComparer<T> if you want a different equality comparison.
Most efficient:
var set = new HashSet<T>(listA);
set.SymmetricExceptWith(listB);
Related
This question already has answers here:
What is a method group in C#?
(5 answers)
Closed 4 years ago.
I am using the below code to check if two lists are equal.
var a = ints1.All(ints2.Contains) && ints1.Count == ints2.Count;
The only thing I do not understand is how does ints2.Contains work. As far as I know, Contains() is a method and takes a parameter. As we can see here, Contains is not taking any parameter.
Note - ints1 and ints2 are two different lists.
That's called a method group. It's basically a shorcut for this:
ints1.All(x => ints2.Contains(x))
This question already has answers here:
How to merge 2 List<T> and removing duplicate values from it in C#
(5 answers)
Closed 6 years ago.
I have 2 lists say list1 and list2. Like
List<int> list1 = {1,2,3};
List<int> list2 = {3,4};
now I want my resultant list to be like
{1,2,3,4}
I have tried this
result= list1.Union(list2).ToList();
But it gives result like
{1,2,3,3,4}
Can anyone help me with this?
It's simple:
var result= list1.Union(list2).Distinct().ToList();
This question already has answers here:
Count the items from a IEnumerable<T> without iterating?
(21 answers)
Closed 8 years ago.
How to check two IEnumerables whether they have same count without running through them individually. Means I do not want to do this Count() == Count().
I would like to find a way to do that in one pass. Any ideas?
This is not possible. Whatever approach you choose, you will have to run through both sequences.
The most straightforward way is to use the Count() method, where would be an O(1) if both sequences are List. In this case, the Count() fails to get the value of list's property called Count.
This question already has answers here:
How to find if an element of a list is in another list?
(5 answers)
Closed 9 years ago.
I apologize if this is an obvious question, but I cannot find the answer.
Say I have the following:
var list1 = new List<int>{1,2,3};
var list2 = new List<int>{3,5,6};
How can I see if ANY element of list1 is contained in list2? So in this case I want to return true because 3 is in both.
Performing nested loops will not work for me, so it would be ideal if there was a:
list1.HasElementIn(list2);
Use Enumerable.Intersect - it produces intersection of both sequences. If intersection is not empty, then there some item which exists in both sequences:
bool isAnyItemInBothLists = list1.Intersect(list2).Any();
One thing to note - thus Intersect is a deferred streaming operator, then you will get result as soon as any common item will be found. So, you don't need to wait until complete intersection will be computed.
This question already has answers here:
Get previous and next item in a IEnumerable using LINQ
(11 answers)
Closed 9 years ago.
Hi I have a dictionary like this
("ABC","X")
("CDE","C")
("EFG","X")
I need to retrieve the items which are in two sides of "C" , How to achieve this with linq ?
Output Expected :
Item = n / output = (n-1)(n+1) items
("ABC","X") and ("EFG","X")
Something like this?
Dict.TakeWhile(e => e.Value = "C").Take(1).Reverse().Take(3).
Then remove the middle one.