comparing values from two different arrays and finding the diff

Discussion in 'Javascript' started by psmahesh@gmail.com, Jul 27, 2006.

  1. Guest

    Hi folks,

    I am comparing two arrays and removing matches from the second array
    from the first array.
    Can someone take a look at this code below and mention if this is okay
    and perhaps if there is a better way to achieve it

    for(i=0;i<arrayA.length;i++){
    for(j=0;j<arrayB.length;j++){
    if(arrayA==arrayB[j])
    {
    arrayA.splice(i,1);
    }
    }
    }

    cheers
    Mahesh
    , Jul 27, 2006
    #1
    1. Advertising

  2. Lee Guest

    said:
    >
    >Hi folks,
    >
    >I am comparing two arrays and removing matches from the second array
    >from the first array.
    >Can someone take a look at this code below and mention if this is okay
    >and perhaps if there is a better way to achieve it
    >
    >for(i=0;i<arrayA.length;i++){
    > for(j=0;j<arrayB.length;j++){
    > if(arrayA==arrayB[j])
    > {
    > arrayA.splice(i,1);
    > }
    > }
    >}



    After you've executed your splice, arrayA takes on the value that
    had been arrayA[i+1]. This new value will not be compared to arrayB[j]
    or to any element of arrayB[] preceding arrayB[j].


    --
    Lee, Jul 27, 2006
    #2
    1. Advertising

  3. JRS: In article <>,
    dated Thu, 27 Jul 2006 11:58:20 remote, seen in
    news:comp.lang.javascript, posted :

    >I am comparing two arrays and removing matches from the second array
    >from the first array.
    >Can someone take a look at this code below and mention if this is okay


    Probably not.

    >and perhaps if there is a better way to achieve it


    Certainly.

    >for(i=0;i<arrayA.length;i++){
    > for(j=0;j<arrayB.length;j++){
    > if(arrayA==arrayB[j])
    > {
    > arrayA.splice(i,1);
    > }
    > }
    >}


    You have design & implementation faults.

    In each J scan, arrayA is looked up; that should be outside the J
    loop.

    In each I & J scan, .length is redetermined for every move. The length
    of one array is constant; that of the other changes only at splice; the
    lengths should be set into variables LA, LB.

    More importantly, taking as an example the case of no matches, you do
    two nested scans, taking time proportional to the product of the lengths
    LA, LB. But if you can .sort the arrays, which takes less than time
    L^2, you can then scan along the arrays in order looking for matches, as
    in a merge sort.

    If you scan backwards, using a while loop, then removing an element
    will not affect the indexing of elements yet to be considered.


    If the elements of the arrays necessarily take the form of identifiers,
    or can be trivially converted to such (e.g. by prepending an 'X'), then
    you can create from A a new Object, C, whose elements are named by the
    values of A and whose values are unimportant. You can then scan B
    similarly, looking to see if C already contains each element. The look-
    ups must take size-dependent time, but will be efficient.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Jul 28, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cyril Vi?ville

    diff Process under diff users

    Cyril Vi?ville, Jun 29, 2004, in forum: Perl
    Replies:
    1
    Views:
    509
    Joe Smith
    Jun 29, 2004
  2. Berrucho
    Replies:
    2
    Views:
    635
    Infant Newbie
    Dec 5, 2003
  3. A Traveler

    Diff CSS styles for diff INPUT TYPE='s?

    A Traveler, Aug 31, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,923
    Steve Fulton
    Aug 31, 2004
  4. darrel
    Replies:
    2
    Views:
    5,062
    darrel
    Dec 23, 2005
  5. Kev Jackson
    Replies:
    2
    Views:
    110
Loading...

Share This Page