473,288 Members | 2,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

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 7669
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jorgen Gustafsson | last post by:
Hi, im trying to write a small progam to compare data in 2 textfiles. I want to search for values that doesnt exist in File2. The result should be "3" in the example below but Im not able to do...
6
by: Raistlin | last post by:
Hey, for c++ does any body have a way to compare two arrays? Say one has the numbers 1-5000, the other has *most* of the numbers from 1-5000, is there a way to display the numbers that are in the...
11
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
1
by: Iain | last post by:
Hi Hopefully I am missing something really simple with this question, but here goes. I have two Bitarrays that I would like to compare. At the moment, I am XORing one with the other and...
12
by: Elijah Bailey | last post by:
I have two char arrays of size k. I want to know which one is bigger (exactly like for instance I compare two ints/longs/etc.). What is the fastest way to do this? k <= 10 usually for my...
2
by: Chaz | last post by:
Hello, I hope someone can help me out. I am going to be taking the third step in a programming class soon(I took the previous two a while ago at a different school) and in an effort to get back up...
18
by: Mike Bartels | last post by:
Hi Everyone! I have two Arrays A and B. Both arrays are byte arrays with 7 bytes each. The contents of array A and B are the same A = {1, 2, 3, 4, 5, 6, 7}; B = {1, 2, 3, 4, 5, 6, 7}; When...
19
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as...
3
by: riauvision | last post by:
Hello, I have problem in writing ms Access vba code to compare two array taken from two different recordset. One is from table: Detail Orders (PK:,POID,Product.Code,Qty) and the other is from table...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.