Hi!

You write that you use nested for. That is a very inefficient way to do it. Suppose n is the size of the array, you would need O(n^2) compares. In your case it is ~7000000*7000000, which is very much.

The following algorithm would be much faster O(n) (or ~7000000*3):

  1. create a temporary hash
  2. add all elements of the first array to that hash; use the array-value as a key and 1 as a value
  3. pass through the second array; if the current value is already in the hash, increase its value by 2
  4. pass through the hash; if all values are 3, array1 is part of array2

Another idea could be to sort both arrays (the perl sort-function is more efficient than a "nested-for-(bubble-)sort") and then work on the sorted arrays.

HTH, Rata

PS.: please be aware that the algorithm above does not work if the first array contains some values more than once!
PPS.: and of course you can improve the algorithm by adding checks on "more than once" in step 3 - and aborting the rest of the loops then

In reply to Re: Searching first array in second array. by Ratazong
in thread Searching first array in second array. by paragkalra

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.