467,910 Members | 1,559 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

extracting differences between two arrays

How can I extract differences between two arrays using Javascript?

I hv for example, apple, orange, pear and another array with apple,
orange and I would like to get pear.

Thanks.

Jul 23 '05 #1
  • viewed: 3647
Share:
4 Replies
Lee
ef*****@epitome.com.sg said:

How can I extract differences between two arrays using Javascript?

I hv for example, apple, orange, pear and another array with apple,
orange and I would like to get pear.


This is really more of a general programming question than javascript:

<html>
<body>
<script type="text/javascript">
function arrayDiff(a,b) {
var seen=new Object();
for (var i=0;i<a.length;i++) {
seen[a[i]]=1;
}
for (i=0;i<b.length;i++) {
if (!seen[b[i]]) {
alert(b[i] + " is in B, but not A");
} else {
seen[b[i]]++;
}
}
for (i in seen) {
if (seen[i]==1) {
alert(i + " is in A, but not B");
}
}
}

arrayDiff(["apple","orange","pear"],["apple","orange","grape"]);
</script>
</body>
</html>

Jul 23 '05 #2
ef*****@epitome.com.sg wrote:
How can I extract differences between two arrays using Javascript?

I hv for example, apple, orange, pear and another array with apple,
orange and I would like to get pear.


Here's one way, with an example:

function aDiff(aRay1, aRay2) {
// initialize aResult, and seed obj, removing dups
var obj = {}, aResult = [];
for (var idx in aRay1) obj[aRay1[idx]] = 1;
// now remove elements common to both
for (idx in aRay2) if (obj[aRay2[idx]]) delete (obj[aRay2[idx]]);
// assemble the result
for (idx in obj) aResult[aResult.length] = idx;
return aResult;
}
var a1 = ['cat', 'mouse', 'dog', 'elephant'];
var a2 = ['bush', 'dog', 'fire hydrant', 'cat'];
alert (aDiff(a1, a2).join(", "));

Csaba Gabor from Vienna

Jul 23 '05 #3
"Csaba Gabor" <Cs***@z6.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
ef*****@epitome.com.sg wrote:
How can I extract differences between two arrays using Javascript?

I hv for example, apple, orange, pear and another array with apple,
orange and I would like to get pear.


Here's one way, with an example:

function aDiff(aRay1, aRay2) {
// initialize aResult, and seed obj, removing dups
var obj = {}, aResult = [];
for (var idx in aRay1) obj[aRay1[idx]] = 1;
// now remove elements common to both
for (idx in aRay2) if (obj[aRay2[idx]]) delete (obj[aRay2[idx]]);
// assemble the result
for (idx in obj) aResult[aResult.length] = idx;
return aResult;
}
var a1 = ['cat', 'mouse', 'dog', 'elephant'];
var a2 = ['bush', 'dog', 'fire hydrant', 'cat'];
alert (aDiff(a1, a2).join(", "));

Csaba Gabor from Vienna


I'm not sure what the original poster's requirements are, but your
solution fails to identify 'fire hydrant' as being different. It seems
like that is what the OP is asking for, but I wonder if he's thought
about it. And if he does want all the differences between two arrays,
how does he want to represent those differences? One huge list? Two
lists showing the elements of array 1 not in array 2 and one showing
elements in array 2 not in array 1?

function aDiff2(arr1, arr2)
{
arr1 = arr1.concat();
arr1.sort();
arr2 = arr2.concat();
arr2.sort();

var arr1Idx = 0;
var arr2Idx = 0;

while (arr1Idx < arr1.length)
{
if (arr2[arr2Idx])
{
if (arr1[arr1Idx] == arr2[arr2Idx])
{
arr1.splice(arr1Idx, 1);
arr2.splice(arr2Idx, 1);
}
else
{
++arr2Idx;
}
}
else
{
++arr1Idx;
}
}

return { diff1:arr1, diff2:arr2 };
}
var a1 = ['cat', 'mouse', 'dog', 'elephant'];
var a2 = ['bush', 'dog', 'fire hydrant', 'cat'];
alert(aDiff2(a1, a2).diff1.join());
alert(aDiff2(a1, a2).diff2.join());

My version breaks down if the array(s) contain(s) duplicate items. I
didn't have to write a general purpose function to deal with this. It
also obviously fails if one array contains "Dog" and the other contains
"dog" (although that could be solved by sorting and comparing without
regard for case).

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #4
Thanks, all the solution was valid, but I opted to use Csaba approach
simple becuase I wanted to consolidate the differences.

But in any case, I learnt all the same from all the codes.

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

36 posts views Thread by Digital Puer | last post: by
1 post views Thread by xllx.relient.xllx | last post: by
1 post views Thread by trpost | last post: by
11 posts views Thread by blangela | last post: by
7 posts views Thread by Umesh | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.