0

I have two lists of strings and want to compare them and extract values in such a way that the below scenario holds true :

list1 = {0,1,2,3,3}
list2 = {0,1,2,3}

Expected O/P : 3 .(I need to ignore the values that have a pair in other list and get only the remaining ones). The item order will remain same in most of scenarios, it would be good if we cover the edge case scenario as well in which the order differs.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Try to use `HashSet` type https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1?redirectedfrom=MSDN&view=netcore-3.1 – Roman Ryzhiy Sep 16 '20 at 06:26
  • Does this answer your question? [Getting unique items from a list](https://stackoverflow.com/questions/1388361/getting-unique-items-from-a-list) – d4zed Sep 16 '20 at 06:28
  • @d4zed No, it doesn't. I am not looking for a way to get unique values in a single list. My issue contains two lists having some elements in common and I want that element which does not form any pair in another list. – Sagar Chopra Sep 16 '20 at 06:30
  • @SagarChopra The answer demonstrates the use of HashSet, which you can adapt to solve your problem (as Roman already suggested) – d4zed Sep 16 '20 at 06:32
  • and what did you try so far? – MakePeaceGreatAgain Sep 16 '20 at 06:41

1 Answers1

-1

Try the below, I think does what you want

List<int> l1 = new List<int>{1, 2, 3, 3};
List<int> l2 = new List<int>{1, 2, 3, 4};
List<int> result = new List<int>();

List<int> l1c = new List<int>();
List<int> l2c = new List<int>();
    
l1c.AddRange(l1);
l2c.AddRange(l2);
    
foreach(int item in l1)
    l2c.Remove(item);
    
foreach(int item in l2)
    l1c.Remove(item);
    
result.AddRange(l1c);
result.AddRange(l2c);
    
Console.WriteLine(string.Join(", ", result));
// this outputs 3, 4

return result;
Mohammad Ali
  • 551
  • 7
  • 17