I have 2 Lists of type string:
Now I need to compare both lists and remove the duplicated items from List1. The modified List1 should have just one item, which is "item1".
foreach loops may work, but what I want to know is there any inbuilt method which does this?
Thanks for the answers guys. I was just thinking what would be the case if I wanted to add the missed out items into the List. So just raised another question similar to this.
Add operation in List<string>
TIA!
I suspect the best approach to use here would be to put the second list into a hash first, i.e.
var hash = new HashSet<TheType>(List2);
List1.RemoveAll(hash.Contains);
This avoids having O(n*m) performance, instead being O(n+m)
Example:
List<int> List1 = new List<int> {1,2,3};
List<int> List2 = new List<int> {2,3};
var hash = new HashSet<int>(List2);
List1.RemoveAll(hash.Contains);
// now List1 just has {1}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With