Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > comparing values from two different arrays and finding the diff

Reply
Thread Tools

comparing values from two different arrays and finding the diff

 
 
psmahesh@gmail.com
Guest
Posts: n/a
 
      07-27-2006
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[i]==arrayB[j])
{
arrayA.splice(i,1);
}
}
}

cheers
Mahesh

 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      07-27-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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[i]==arrayB[j])
> {
> arrayA.splice(i,1);
> }
> }
>}



After you've executed your splice, arrayA[i] 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].


--

 
Reply With Quote
 
 
 
 
Dr John Stockton
Guest
Posts: n/a
 
      07-28-2006
JRS: In article <(E-Mail Removed). com>,
dated Thu, 27 Jul 2006 11:58:20 remote, seen in
news:comp.lang.javascript, (E-Mail Removed) 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[i]==arrayB[j])
> {
> arrayA.splice(i,1);
> }
> }
>}


You have design & implementation faults.

In each J scan, arrayA[i] 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 4.43 How do I compute the difference of two arrays? How do I compute the intersection of two arrays? PerlFAQ Server Perl Misc 0 02-02-2011 05:00 AM
Two arrays: return only values that exist in both arrays? Joshua Muheim Ruby 8 11-23-2007 07:04 PM
two arrays problem (although different from the other two arrays) Kev Jackson Ruby 2 03-29-2006 03:58 PM
Comparing values between two arrays darrel ASP .Net 2 12-23-2005 05:11 PM
diff Process under diff users Cyril Vi?ville Perl 1 06-29-2004 06:22 PM



Advertisments