I think you really meant to write:
<script type="text/javascript">
var a = ['a','b','c','d','e'];
var b = ['a','d','e','c'];
</script>
In any case, you can sort the arrays and get the values from one that aren't in the other and vice versa, then concatenate the two sets into one. You seem spoilt for choice, so here's a nice basic javascript version that should work in most browsers. Using new features of the latest browsers is certain to fail in older browsers.
// Compares a to b. Returns all the elements in a that are not in b
// If c provided, add unique elements to c
function getUnique(a, b, c) {
var c = c || [];
var ta = a.slice().sort();
var tb = b.slice().sort();
var x, y, found = false;
for (var i=0, iLen=ta.length; i<iLen; i++) {
x = ta.shift();
for (var j=0; j<tb.length && !found; j++) { // j.length changes each loop
if (tb[j] == x) {
tb.splice(j,1); // Remove match from b
found = true;
}
}
if (!found) {
c.push(x); // If no match found, store in result
}
found = false;
}
return c;
}
var a = ['a','b','d'];
var b = ['b','e'];
var d = getUnique(a, b);
alert(d);
var c = getUnique(b,a,d);
alert(d);
But your comment on the first answer indicates that you want the elements that are common to both arrays, which is simpler:
function getCommon(a, b) {
var c = [];
var ta = a.slice().sort();
var tb = b.slice().sort();
var t, found;
for (var i=0, iLen=ta.length; i<iLen; i++) {
t = ta[i];
found = false;
for (var j=0, jLen=tb.length; j<jLen && !found; j++) {
if (t == tb[j]) {
c.push(tb.splice(j,1));
found = true;
}
}
}
return c;
}
alert(getCommon(a, b));
You need to work out what to do with duplicates. In the first case, a duplicates will be treated as unique if there isn't a duplicate in the other array. In the above, duplicates don't matter unless they are duplicated in both arrays.