I have listA, listB. listA is subset of listB. For example, 1 element is deleted and 2 elements are added to listB. Then if listA contains the element that was deleted from listB, delete it from listA. Also, listA should add the newly added elements.
At present I am using foreach{ if(list.contains) } two times. Once to add and once to delete. This will be of O(2n), which is ok.
But is there a best way to do this mostly with O(n) in LINQ/any other way?.
To be more clear:
Actually I have a list of custom class.
From which I am forming listA in above question(using one field of that). ListB is just list of string which I get from web service.
Code:
//First foreach loop which I was taking about.
foreach (string A in listA)
{
if (listB.Contains(A)
{
}
else
{
//getting items that are added to listB
}
}
//Second foreach loop which i was taking about.
foreach (string A in listB)
{
if (listA.Contains(A)
{
}
else
{
//getting items that are deleted from listB
}
}
And then I am updating that List<custom class> accordingly. My main question is instead of using two foreach loops can I do something better?