Nodejs Array Different diffArray(arr1, arr2)

Here you can find the source of diffArray(arr1, arr2)

Method Source Code

/* Intermediate Algorithm: Diff Two Arrays

We'll pass you an array of two numbers. Return the sum of those two numbers and 
all numbers between them./*from  w  ww  .  j av a2s  .  c o  m*/

The lowest number will not always come first.

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful hints:

Math.max()
Math.min()
Array.prototype.reduce()

Written by: Sean M Hamlet
https://www.freecodecamp.com/seanmhamlet
*/

function diffArray(arr1, arr2) {
  var diff1 = [];
  var diff2 = [];
  // Same, same; but different.
  
  // Determine if any values in arr2 are in arr1
  diff1 = arr1.filter(function(value) { return arr2.indexOf(value) < 0;});
  
  // Determine if any values in arr1 are in arr2
  diff2 = arr2.filter(function(value) { return arr1.indexOf(value) < 0;});
  
  // Concatenate values not in both arrays 
  return diff1.concat(diff2);
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

Related

  1. diff(init)
    Array.prototype.diff = function (init) {
        init = init || 0;
        var g = this;
        return this.reduce(function(prev,cur) {
            prev.push(cur - (prev.length > 0 ? g[prev.length-1] : init));
            return prev;
        },[]);
    
  2. diff(part)
    Array.prototype.diff = function(part) {
        return this.filter(function(element) {return part.indexOf(element) < 0;});
    };
    
  3. diff1(compare)
    Array.prototype.diff1 = function(compare) {
        return this.filter(function(elem) {return elem!=compare;})
    
  4. diffArrays(a)
    function getJSON(yourUrl) {
        var Httpreq = new XMLHttpRequest(); 
        Httpreq.open("GET", yourUrl, false);
        Httpreq.send(null);
        return Httpreq.responseText;
    Array.prototype.diffArrays = function(a) {
        return this.filter(function(i) {
            return a.indexOf(i) < 0;
    ...
    
  5. diffArray(arr1, arr2)
    function diffArray(arr1, arr2) {
      var newArr = arr1.concat(arr2);
      function checkItems(item) {
        if (arr1.indexOf(item) === -1 || arr2.indexOf(item) === -1) {
          return item;
      return newArr.filter(checkItems);
    diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
    
  6. diffArray(arr1, arr2)
    function diffArray(arr1, arr2) {
      let newArr1 = [];
      let newArr2 = [];
      let sliced = [];
      for (let y = 0; y < arr1.length; y++) {
        if (arr2.includes(arr1[y]) === false) {
          newArr1.push(y);
      for (let i = 0; i < newArr1.length; i++) {
        sliced.push(arr1.splice(newArr1[0], 1));
      for (let x = 0; x < arr2.length; x++) {
        if (arr1.includes(arr2[x]) === false) {
          newArr2.push(x);
      for (let j = 0; j < newArr2.length; j++) {
        sliced.push(arr2.splice(newArr2[0], 1));
      let finalArr = [].concat.apply([], sliced);
        return finalArr;
    diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
    
  7. difference(t)
    Array.prototype.difference = function(t)
        var t = t.toHash();
        var r = [];
        this.forEach(function (item) { if (!t.containsKey(item)) r.push(item); });
        return r;