Note that there are some explanatory texts on larger screens.

plurals
  1. POTake & remove elements from collection
    primarykey
    data
    text
    <p>What's the most performant way to remove n elements from a collection and add those removed n elements to an already existing, different, collection?</p> <p>Currently I've got this:</p> <pre><code>var entries = collection.Take(5).ToList(); foreach(var entry in entries) collection.Remove(entry); otherCollection.AddRange(entries); </code></pre> <p>However, this doesn't look performant at all to me (<em>multiple</em> linear algorithms instead of only one).</p> <p>A possible solution may of course change the collection implementation - as long as the following requirements are met:</p> <ul> <li><code>otherCollection</code> must implement <code>IEnumerable&lt;T&gt;</code>, it is currently of type <code>List&lt;T&gt;</code></li> <li><code>collection</code> must implement <code>ICollection&lt;T&gt;</code>, it is currently of type <code>LinkedList&lt;T&gt;</code></li> </ul> <p>Hint: entries do not necessarily implement <code>Equals()</code> or <code>GetHashCode()</code>.</p> <p>What's the most performant way to reach my goal?</p> <hr> <p>As it has been obviously too hard to understand my performance considerations, here once more my code example:</p> <pre><code>var entries = collection.Take(1000).ToList(); // 1000 steps foreach(var entry in entries) // 1000 * 1 steps (as Remove finds the element always immediately at the beginning) collection.Remove(entry); otherCollection.AddRange(entries); // another 1000 steps </code></pre> <p>= 3000 steps in total => I want to reduce it to a single 1000 steps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload