Symmetric difference between two array - Node.js Data Structure

Node.js examples for Data Structure:Set

Description

Symmetric difference between two array

Demo Code


function relative_complement(A, B) {
  return A.filter(function(elem) {
    return B.indexOf(elem) == -1;
  });//from   w w  w  . java 2 s  .  c o m
}

function symmetric_difference(A, B) {
  return concat(relative_complement(A, B), relative_complement(B, A));
}

function concat(arr1, arr2) {
  for (i in arr2)
    arr1.push(arr2[i]);
  return arr1;
}

String.prototype.startsWith = function(needle){
    return(this.indexOf(needle) == 0);
}

Related Tutorials