Skip to main content
deleted 37 characters in body
Source Link
Tushar
  • 3k
  • 1
  • 21
  • 28

Create a function on Array.prototype that'll accept an array and filter the array by checking if the element is in the other array.

// Define a function on Array prototype
Array.prototype.removeDuplicatesarrayDiff = function (arr) {
    return this.filter(function (v) {
        return arr.indexOf(v) === -1;
    });
};

function diff(arr1, arr2) {
    // 1. Remove elements that are in arr2 from arr1
    //    i.e. arr1.removeDuplicatesarrayDiff(arr2)
    // 2. Remove elements that are in arr1 from arr2
    //    i.e. arr2.removeDuplicatesarrayDiff(arr1)
    // 3. Concat the result of both the arrays

    return arr1.removeDuplicatesarrayDiff(arr2).concat(arr2.removeDuplicatesarrayDiff(arr1));
}

// Define a function on Array prototype
Array.prototype.removeDuplicates = function(arr) {
  return this.filter(function(v) {
    return arr.indexOf(v) === -1;
  });
};

function diff(arr1, arr2) {
  return arr1.removeDuplicates(arr2).concat(arr2.removeDuplicates(arr1));
}

var arr = diff([1, 3, 4, 2, 5], [2, 45, 7, 42, 1, 6]);
document.write(JSON.stringify(arr));

Create a function on Array.prototype that'll accept an array and filter the array by checking if the element is in the other array.

// Define a function on Array prototype
Array.prototype.removeDuplicates = function (arr) {
    return this.filter(function (v) {
        return arr.indexOf(v) === -1;
    });
};

function diff(arr1, arr2) {
    // 1. Remove elements that are in arr2 from arr1
    //    i.e. arr1.removeDuplicates(arr2)
    // 2. Remove elements that are in arr1 from arr2
    //    i.e. arr2.removeDuplicates(arr1)
    // 3. Concat the result of both the arrays

    return arr1.removeDuplicates(arr2).concat(arr2.removeDuplicates(arr1));
}

// Define a function on Array prototype
Array.prototype.removeDuplicates = function(arr) {
  return this.filter(function(v) {
    return arr.indexOf(v) === -1;
  });
};

function diff(arr1, arr2) {
  return arr1.removeDuplicates(arr2).concat(arr2.removeDuplicates(arr1));
}

var arr = diff([1, 3, 4, 2, 5], [2, 45, 7, 42, 1, 6]);
document.write(JSON.stringify(arr));

Create a function on Array.prototype that'll accept an array and filter the array by checking if the element is in the other array.

// Define a function on Array prototype
Array.prototype.arrayDiff = function (arr) {
    return this.filter(function (v) {
        return arr.indexOf(v) === -1;
    });
};

function diff(arr1, arr2) {
    // 1. Remove elements that are in arr2 from arr1
    //    i.e. arr1.arrayDiff(arr2)
    // 2. Remove elements that are in arr1 from arr2
    //    i.e. arr2.arrayDiff(arr1)
    // 3. Concat the result of both the arrays

    return arr1.arrayDiff(arr2).concat(arr2.arrayDiff(arr1));
}

// Define a function on Array prototype
Array.prototype.removeDuplicates = function(arr) {
  return this.filter(function(v) {
    return arr.indexOf(v) === -1;
  });
};

function diff(arr1, arr2) {
  return arr1.removeDuplicates(arr2).concat(arr2.removeDuplicates(arr1));
}

var arr = diff([1, 3, 4, 2, 5], [2, 45, 7, 42, 1, 6]);
document.write(JSON.stringify(arr));

added 1 character in body
Source Link
Tushar
  • 3k
  • 1
  • 21
  • 28

Create a function on Array.prototype that'll accept an array and filter the array by checking if the element is in the other array.

// Define a function on Array prototype
Array.prototype.removeDuplicates = function (arr) {
    return this.filter(function (v) {
        return arr.indexOf(v) === -1;
    });
};

function diff(arr1, arr2) {
    // 1. Remove elements that are in arr2 from arr1
    //    i.e. arr1.removeDuplicates(arr2)
    // 2. Remove elements that are in arr1 from arr2
    //    i.e. arr2.removeDuplicates(arr1)
    // 3. Concat the result of both the arrays

    return arr1.removeDuplicates(arr2).concat(arr2.removeDuplicates(arr1));
}

// Define a function on Array prototype
Array.prototype.removeDuplicates = function(arr) {
  return this.filter(function(v) {
    return arr.indexOf(v) === -1;
  });
};

function diff(arr1, arr2) {
  return arr1.removeDuplicates(arr2).concat(arr2.removeDuplicates(arr1));
}

var arr = diff([1, 3, 4, 2, 5], [2, 45, 7, 42, 1, 6]);
document.write(JSON.stringify(arr));

Create a function on Array.prototype that'll accept an array and filter the array by checking if the element is in the other array.

// Define a function on Array prototype
Array.prototype.removeDuplicates = function (arr) {
    return this.filter(function (v) {
        return arr.indexOf(v) === -1;
    });
}

function diff(arr1, arr2) {
    // 1. Remove elements that are in arr2 from arr1
    //    i.e. arr1.removeDuplicates(arr2)
    // 2. Remove elements that are in arr1 from arr2
    //    i.e. arr2.removeDuplicates(arr1)
    // 3. Concat the result of both the arrays

    return arr1.removeDuplicates(arr2).concat(arr2.removeDuplicates(arr1));
}

Create a function on Array.prototype that'll accept an array and filter the array by checking if the element is in the other array.

// Define a function on Array prototype
Array.prototype.removeDuplicates = function (arr) {
    return this.filter(function (v) {
        return arr.indexOf(v) === -1;
    });
};

function diff(arr1, arr2) {
    // 1. Remove elements that are in arr2 from arr1
    //    i.e. arr1.removeDuplicates(arr2)
    // 2. Remove elements that are in arr1 from arr2
    //    i.e. arr2.removeDuplicates(arr1)
    // 3. Concat the result of both the arrays

    return arr1.removeDuplicates(arr2).concat(arr2.removeDuplicates(arr1));
}

// Define a function on Array prototype
Array.prototype.removeDuplicates = function(arr) {
  return this.filter(function(v) {
    return arr.indexOf(v) === -1;
  });
};

function diff(arr1, arr2) {
  return arr1.removeDuplicates(arr2).concat(arr2.removeDuplicates(arr1));
}

var arr = diff([1, 3, 4, 2, 5], [2, 45, 7, 42, 1, 6]);
document.write(JSON.stringify(arr));

Source Link
Tushar
  • 3k
  • 1
  • 21
  • 28

Create a function on Array.prototype that'll accept an array and filter the array by checking if the element is in the other array.

// Define a function on Array prototype
Array.prototype.removeDuplicates = function (arr) {
    return this.filter(function (v) {
        return arr.indexOf(v) === -1;
    });
}

function diff(arr1, arr2) {
    // 1. Remove elements that are in arr2 from arr1
    //    i.e. arr1.removeDuplicates(arr2)
    // 2. Remove elements that are in arr1 from arr2
    //    i.e. arr2.removeDuplicates(arr1)
    // 3. Concat the result of both the arrays

    return arr1.removeDuplicates(arr2).concat(arr2.removeDuplicates(arr1));
}