Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Improved algorithm

Tags:

c#

generics

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

like image 540
user196546 Avatar asked Nov 18 '09 19:11

user196546


1 Answers

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.

like image 200
Konrad Rudolph Avatar answered Oct 05 '22 22:10

Konrad Rudolph