<p>I got a Function that returns a <code>Collection&lt;string&gt;</code>, and that calls itself recursively to eventually return one big <code>Collection&lt;string&gt;</code>.</p>

<p>Now, i just wonder what the best approach to merge the lists? <code>Collection.CopyTo()</code> only copies to string[], and using a <code>foreach()</code> loop feels like being inefficient. However, since I also want to filter out duplicates, I feel like i'll end up with a foreach that calls <code>Contains()</code> on the <code>Collection</code>.</p>

<p>I wonder, is there a more efficient way to have a recursive function that returns a list of strings without duplicates? I don't have to use a <code>Collection</code>, it can be pretty much any suitable data type.</p>

<p>Only exclusion, I'm bound to Visual Studio 2005 and .net 3.0, so no LINQ.</p>

<p><strong>Edit:</strong> To clarify: The Function takes a user out of Active Directory, looks at the Direct Reports of the user, and then recursively looks at the direct reports of every user. So the end result is a List of all users that are in the "command chain" of a given user.Since this is executed quite often and at the moment takes 20 Seconds for some users, i'm looking for ways to improve it. Caching the result for 24 Hours is also on my list btw., but I want to see how to improve it before applying caching.</p>

[{'answer_id': 56092, 'author': 'jfs', 'author_id': 718, 'author_profile': 'https://Stackoverflow.com/users/718', 'pm_score': 1, 'selected': False, 'text': '<p>I think <a href="http://msdn.microsoft.com/en-us/library/bb359438.aspx" rel="nofollow noreferrer"><code>HashSet&lt;T&gt;</code></a> is a great help.</p>\n\n<blockquote>\n  <p>The <code>HashSet&lt;T&gt;</code> class provides\n  high performance set operations. A set\n  is a collection that contains no\n  duplicate elements, and whose elements\n  are in no particular order.</p>\n</blockquote>\n\n<p>Just add items to it and then use CopyTo.</p>\n\n<hr>\n\n<p><strong>Update</strong>: <code>HashSet&lt;T&gt;</code> is in .Net 3.5</p>\n\n<p>Maybe you can use <a href="http://msdn.microsoft.com/en-us/library/xfhwa508.aspx" rel="nofollow noreferrer"><code>Dictionary&lt;TKey, TValue&gt;</code></a>. Setting a duplicate key to a dictionary will not raise an exception.</p>\n'}, {'answer_id': 56100, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 5, 'selected': True, 'text': "<p>If you're using List&lt;> you can use .AddRange to add one list to the other list.</p>\n\n<p>Or you can use yield return to combine lists on the fly like this:</p>\n\n<pre><code>public IEnumerable&lt;string&gt; Combine(IEnumerable&lt;string&gt; col1, IEnumerable&lt;string&gt; col2)\n{\n    foreach(string item in col1)\n        yield return item;\n\n    foreach(string item in col2)\n        yield return item;\n}\n</code></pre>\n"}, {'answer_id': 56104, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 1, 'selected': False, 'text': '<p>You might want to take a look at <a href="http://www.codeproject.com/KB/recipes/sets.aspx" rel="nofollow noreferrer">Iesi.Collections</a> and <a href="http://www.codeproject.com/KB/recipes/GenericISet.aspx?display=Print" rel="nofollow noreferrer">Extended Generic Iesi.Collections</a> (because the first edition was made in 1.1 when there were no generics yet).</p>\n\n<p>Extended Iesi has an ISet class which acts exactly as a HashSet: it enforces unique members and does not allow duplicates.</p>\n\n<p>The nifty thing about Iesi is that it has set operators instead of methods for merging collections, so you have the choice between a union (|), intersection (&amp;), XOR (^) and so forth.</p>\n'}, {'answer_id': 56108, 'author': 'Matthew M. Osborn', 'author_id': 5235, 'author_profile': 'https://Stackoverflow.com/users/5235', 'pm_score': 1, 'selected': False, 'text': '<p>Can you pass the Collection into you method by refernce so that you can just add items to it, that way you dont have to return anything.  This is what it might look like if you did it in c#.</p>\n\n<pre><code>class Program\n{\n    static void Main(string[] args)\n    {\n        Collection&lt;string&gt; myitems = new Collection&lt;string&gt;();\n        myMthod(ref myitems);\n        Console.WriteLine(myitems.Count.ToString());\n        Console.ReadLine();\n    }\n\n    static void myMthod(ref Collection&lt;string&gt; myitems)\n    {\n        myitems.Add("string");\n        if(myitems.Count &lt;5)\n            myMthod(ref myitems);\n    }\n}\n</code></pre>\n\n<p>As Stated by @Zooba Passing by ref is not necessary here, if you passing by value it will also work.</p>\n'}, {'answer_id': 56345, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>As far as merging goes:</p>\n\n<blockquote>\n  <p>I wonder, is there a more efficient\n  way to have a recursive function that\n  returns a list of strings without\n  duplicates? I don't have to use a\n  Collection, it can be pretty much any\n  suitable data type.</p>\n</blockquote>\n\n<p>Your function assembles a return value, right? You're splitting the supplied list in half, invoking self again (twice) and then merging those results. </p>\n\n<p>During the merge step, why not just check before you add each string to the result? If it's already there, skip it.</p>\n\n<p>Assuming you're working with sorted lists of course.</p>\n"}]
['https://Stackoverflow.com/questions/56078', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/91/']