Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This operation can be called a symmetric difference.</p> <p>You need a different data structure, like a hash table. Add the intersection of both sets to it, then difference the intersection from each set.</p> <p><strong>UPDATE:</strong></p> <p>I got a bit of time to try this in code. I used <code>HashSet&lt;T&gt;</code> with a set of 50,000 strings, from 2 to 10 characters long with the following results:</p> <blockquote> <p><strong>Original</strong>: 79499 ms</p> <p><strong>Hashset</strong>: 33 ms</p> </blockquote> <p>BTW, there is a method on HashSet called <code>SymmetricExceptWith</code> which I thought would do the work for me, but it actually adds the different elements from both sets to the set the method is called on. Maybe this is what you want, rather than leaving the initial two sets unmodified, and the code would be more elegant.</p> <p>Here is the code:</p> <pre><code>using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; class Program { static void Main(string[] args) { // foo and bar have some identical elements (given a case-insensitive match) var foo = getRandomStrings(); var bar = getRandomStrings(); var timer = new Stopwatch(); timer.Start(); // remove non matches var f = foo.Where(x =&gt; !bar.Contains(x)).ToList(); var b = bar.Where(x =&gt; !foo.Contains(x)).ToList(); timer.Stop(); Debug.WriteLine(String.Format("Original: {0} ms", timer.ElapsedMilliseconds)); timer.Reset(); timer.Start(); var intersect = new HashSet&lt;String&gt;(foo); intersect.IntersectWith(bar); var fSet = new HashSet&lt;String&gt;(foo); var bSet = new HashSet&lt;String&gt;(bar); fSet.ExceptWith(intersect); bSet.ExceptWith(intersect); timer.Stop(); var fCheck = new HashSet&lt;String&gt;(f); var bCheck = new HashSet&lt;String&gt;(b); Debug.WriteLine(String.Format("Hashset: {0} ms", timer.ElapsedMilliseconds)); Console.WriteLine("Sets equal? {0} {1}", fSet.SetEquals(fCheck), bSet.SetEquals(bCheck)); //bSet.SetEquals(set)); Console.ReadKey(); } static Random _rnd = new Random(); private const int Count = 50000; private static List&lt;string&gt; getRandomStrings() { var strings = new List&lt;String&gt;(Count); var chars = new Char[10]; for (var i = 0; i &lt; Count; i++) { var len = _rnd.Next(2, 10); for (var j = 0; j &lt; len; j++) { var c = (Char)_rnd.Next('a', 'z'); chars[j] = c; } strings.Add(new String(chars, 0, len)); } return strings; } } </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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