I have been asked at interview (C# 3.0) to provide a logic to remove a list of items from a list.
I responded
int[] items={1,2,3,4};
List<int> newList = new List<int>() { 1, 2, 3, 4, 5, 56, 788, 9 };
newList.RemoveAll((int i) => { return items.Contains(i); });
1) The interviewer replied that the algorithm i had employed will gradually take time if the items grow and asked me to give even better and faster one.What would be the efficient algorithm ?
2) How can i achieve the same using LINQ?
3) He asked me to provide an example for Two-Way-Closure? (General I am aware of closure, what is Two-Way-Closure?, I replied there is no such term exists,but he did not satisfy).
EDIT Better solution: use Except
which isn’t symmetrical, unlike Intersect
.
1 & 2: you can use the Intersect
extension method to do this. However, if your second array contains elements not found in the first one, these will then be in the resulting list: Intersect
works symmetrically.
As for “two-way closure”, I’ve never heard of this term and I rather doubt that it’s an established technical term.
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