Comparing two lists distinct value C# [duplicate] - c#

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();

Related

Combining elements of the same index from two lists in C# [duplicate]

This question already has an answer here:
Combining two lists with different elements to one list with all elements and SAME index in c#
(1 answer)
Closed 9 months ago.
I have two lists:
list1=[1,2,3]
list2=["Ham","Cheese","Bacon"]
I want to take each element with the same index in each list and combine them into a resulting list like the below:
["1 Ham", "2 Cheese","3 Bacon"]
Where either list can be empty.
I tried the below and it worked for me:
var list1= [1,2,3];
var list2=["Ham","Cheese","Bacon"];
var list3=list1.Count>0? list1.Zip(list2??Enumerable.Empty<string>(),(first,second)=> first+ " "+ second).ToList():list2;

.NET list access index [duplicate]

This question already has answers here:
Is the order of elements on a C# List<T> deterministic?
(3 answers)
Closed 8 years ago.
Follow up question from How to select List<> by its index and return the content?
Can someone please comment if the elements in the list are expected to retain order? In the example above, is "Belly Buster" always expected to be at index 1 or are there cases where that might not be true?
List<string> pizzas = new List<string>();
pizzas.Add("Angus Steakhouse");
pizzas.Add("Belly Buster");
pizzas.Add("Pizza Bianca");
pizzas.Add("Classic Cheese");
pizzas.Add("Friday Special");
string result = pizzas[1]; // result is equal to "Belly Buster"
index starts at 0 so yes if the code stays the same..belly buster will always be at 1

C# Check if any int in list matches any int in another list [duplicate]

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.

C# Linq Query to get between items [duplicate]

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.

how to select elements that not intersec? [duplicate]

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);

Categories

Resources