Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since you specifically noted that it could be in several languages, I'll leave my answer in pseudocode so you can adapt to the language of your choice.</p> <p>Since you are matching an array-to-array, performance is going to vary a lot based on your implementation, so trying several ways and considering exactly when/how/how-often this will be used in a good idea.</p> <p>The simple way is to leave the data as-is and run an O(n^2) search:</p> <pre><code>for (every element in array X) for (every element in array Y) if (current X == current Y) add current X to results return results </code></pre> <p>If you sorted the arrays first (a sorting algorithm such a squicksort is implemented for you in many languages, check your documentation!), then the actual matching is quicker. Use whatever string-comparison your language has:</p> <pre><code>Sort array X Sort array Y Let A = first element of X Let B = first element of Y while (A and B are in array) if (A &gt; B) Next B else if (A &lt; B) Next A else //match! Add A to results Next A Next B //handle case where one array is larger (trivial loop) return results </code></pre> <p>Now the important part to the above solution is if the sorting of the arrays saved time versus just an ordinary O(n^2) sort. Usually, moving elements around in arrays is fast whereas string comparisons are not, so it may be worth it. Again, try both.</p> <p>Finally, there's this crazy algorithm the Mailinator guy dreamed up for doing huge string comparisons in constant time using some awesome data structures. Never tried it myself, but it must work since he runs his whole site on very low-end hardware. He blogged about it <a href="http://mailinator.blogspot.com/2008/01/how-to-search-for-word-pen1s-in-185.html" rel="nofollow">here</a> if you're interested. (Note: the blog post is about filtering spam, so some words in the post may be slightly NSFW.)</p>
 

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