Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, there is a faster way, provided you're not space-constrained. (See <a href="http://en.wikipedia.org/wiki/Space-time_tradeoff" rel="noreferrer">space/time tradeoff</a>.)</p> <p>The algorithm:</p> <p>Just insert all the elements in Set2 into a hashtable (in C# 3.5, that's a <a href="http://msdn.microsoft.com/en-us/library/bb359438.aspx" rel="noreferrer">HashSet&lt;string&gt;</a>), and then go through all the elements of Set1 and check if they're in the hashtable. This method is faster (Θ(m + n) time complexity), but uses O(n) space.</p> <p>Alternatively, just say:</p> <pre><code>bool isSuperset = new HashSet&lt;string&gt;(set2).IsSupersetOf(set1); </code></pre> <hr> <p><strong>Edit 1:</strong></p> <p>For those people concerned about the possibility of duplicates (and hence the misnomer "set"), the idea can easily be extended:</p> <p>Just make a new <code>Dictionary&lt;string, int&gt;</code> representing the count of each word in the super-list (add one to the count each time you see an instance of an existing word, and add the word with a count of 1 if it's not in the dictionary), and then go through the sub-list and decrement the count each time. If every word exists in the dictionary <em>and</em> the count is never zero when you try to decrement it, then the subset is in fact a sub-list; otherwise, you had too many instances of a word (or it didn't exist at all), so it's not a real sub-list.</p> <hr> <p><strong>Edit 2:</strong></p> <p>If the strings are very big and you're concerned about space efficiency, and an algorithm that works with (very) high probability works for you, then try storing a <em>hash</em> of each string instead. It's technically not <em>guaranteed</em> to work, but the probability of it not working is pretty darn low.</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.
    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.
 

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