469,282 Members | 2,025 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

comparing values from two different arrays and finding the diff

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

Jul 27 '06 #1
1 7474
JRS: In article <11**********************@75g2000cwc.googlegroups. com>,
dated Thu, 27 Jul 2006 11:58:20 remote, seen in
news:comp.lang.javascript, ps******@gmail.com 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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 28 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jorgen Gustafsson | last post: by
6 posts views Thread by Raistlin | last post: by
11 posts views Thread by Peter | last post: by
1 post views Thread by Iain | last post: by
12 posts views Thread by Elijah Bailey | last post: by
2 posts views Thread by Chaz | last post: by
19 posts views Thread by Ole Nielsby | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.