What you're asking about is the Shortest Common Superstring problem, for which there is no algorithm that works for all cases. But it is a common problem (in compression and DNA sequencing) and several approximation algorithms are well-known.
"Greedy" algorithms are generally accepted to be the most effective (as in, they have the least-bad worst-case).
Have a read of the paper Approximation Algorithms for the Shortest Common Superstring Problem by Jonathan Turner for much more information.
The best you can do is reduce the number of comparisons to the number of letters in the dictionary.
sought: a,e,o,g,z,k,l,j,w,n
make index of alphabet where sought keys have value 1, the rest: 0.
index={a:1, b:0, c:0, d:0, e:1, f:0, g:1...}
Iterate over each word of dictionary. Add value of the index to sum of that word. Remember word position and value if it's greater than best.
max=0;
max_index=0;
foreach(dictionary as position=>word)
{
sum=0;
foreach(word as letter)
{
sum += index[letter];
}
if(sum > max)
{
max = sum;
max_index = position;
}
}
max_index points to the word with maximum of the letters.
Some optimizations may be skipping words shorter than the current max, or starting with dictionary sorted by word length and stopping once word length drops to max currently found.
This is assuming letters from the list are allowed to repeat any number of times. If they are not, make the index contain number of given type of letters, increment sum by 1 on each find of non-zero index value and decrement the index. (reset index on each line.)
In this time optimizations could be, on top of the previous ones: abort checking word if less than max-sum letters remain, abort operation if word with all letters is found.
Best Answer
Sort your array, but maintain the original index number. Then you can loop through it once, each value can be instantly matched with the set of indexes stored against the matching point.
ie. loop through your array and store a key-value pair (where the key is array[n] and the value is n). A single iteration of this container will show you all matching indexes for the current value.