0

I know that you can remove 1 of the duplicate number out of an array but is there a way you can remove the number if they're duplicate? So far, below code is what I have. I wanted to use for loop to remove the number out of the array if they're equal, but I dont think I coded it correctly or it's not complete. I want it to return [3, 4, 5]

function sym(args) {
  var array = [];
  var join;
    for(var i = 0; i < arguments.length; i++){
      array.push(arguments[i]);
      join = array[0].concat(array[1]); 
    } 
  join.sort();
    for(var j = 0; j < join.length; j++) {
      if(join[j] === join[j+1]) {

        var removed = join.splice(j, join[j+2]);                 
        console.log(removed);
      }

    }  

  return join;

}

sym([1, 2, 3], [5, 2, 1, 4]);
jwatts1980
  • 6,991
  • 2
  • 24
  • 39
jyoon006
  • 599
  • 2
  • 7
  • 14
  • 3
    possible duplicate of [How to merge two arrays in Javascript and de-duplicate items](http://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items) – tremor May 17 '15 at 00:57
  • 2
    to the people voting to close: he's not asking to dedupe: he's asking to *remove **all** of `n`* if there's more than one `n` – Crayon Violent May 17 '15 at 01:12
  • possible duplicate of [How to know if two arrays have the same values](http://stackoverflow.com/questions/6229197/how-to-know-if-two-arrays-have-the-same-values) – MaxZoom May 17 '15 at 01:37
  • possible duplicate of [JavaScript array difference](http://stackoverflow.com/questions/1187518/javascript-array-difference) – Jo Liss May 17 '15 at 14:29

3 Answers3

1

Here's my take

function sym() {
  var vals = {};
  var rarray= [];
  var a=arguments;
  for (var i=0,l=a.length;i<l;i++) {
    if (a[i] instanceof Array) {
      for (var n=0,ln=a[i].length;n<ln;n++) {
        vals[a[i][n]]=vals[a[i][n]]||[];
        vals[a[i][n]].push(a[i][n]);
      }
    }
  }
  for (var i in vals) {
    if (vals.hasOwnProperty(i)) {
      if (vals[i].length===1)
        rarray.push(i);
    }
  }
  return rarray;
}

Examples:

sym([1, 2, 3], [5, 2, 1, 4]);
// return: ["3", "4", "5"]

sym([1, 2, 3], [5, 2, 1, 4],[4,6,7,8],[8,4]);
// ["3", "5", "6", "7"]

sym([1,2],[1]);
// return: ["2"]
Crayon Violent
  • 30,524
  • 3
  • 51
  • 74
1
var sym = function (ar1, ar2) {
    return ar1
        .concat(ar2)
        .sort(function (a, b) { return a - b; })
        .filter(function (elem, i, ar) {
            return ar[i-1] !== elem && ar[i+1] !== elem;
        });
    }
jcz
  • 861
  • 2
  • 9
  • 13
  • Rad. Some people (myself included, obviously, judging by my own answer) get too caught up on growth rates, and too quickly avoid the clean/simple sorting solution (which, the sorting solution, is faster in most practical scenarios as well!) Totally love it. The only thing I'd caution on here is Array bounds checking: if the sort sends `undefined` to either end of the Array, it will wrongfully be removed. – DRobinson May 17 '15 at 18:29
  • @DRobinson I agree about the `undefined`, but I assumed (from the question) that the arrays are filled only with numbers. – jcz May 17 '15 at 19:10
0

I like Crayon Violent's solution, but I don't see the point in maintaining arrays of duplicate items when you can simply count them.

This provides a large performance increase (jsperf), while also simplifying the code.

function sym() {
  var occurrences = {};
  var inputArrays = arguments;
  var uniqueItems = [];

  function addOccurrences(arr) {
    for (var i = 0, len=arr.length; i < len; i++) {
      occurrences[arr[i]] = 1 + (occurrences[arr[i]] || 0);
    }
  }

  for (var i=0, len=inputArrays.length; i < len; i++) {
    if (inputArrays[i] instanceof Array) {
      addOccurrences(inputArrays[i]);
    }
  }

  for (var item in occurrences) {
    if (occurrences[item] === 1) {
        uniqueItems.push(item);
    }
  }
  return uniqueItems;
}

Which can be made nicer if you happen to have underscore or lodash in your project:

function sym() {
  var inputArrays = _.filter(arguments, _.isArray);
  var occurrences = {};

  function addOccurrences(arr) {
    _.forEach(arr, function(item){
      occurrences[item] = 1 + (occurrences[item] || 0);
    });
  }

  _.forEach(inputArrays, addOccurrences);

  // Select the items with one occurence, return the keys (items)
  return _.filter(_.keys(occurrences), function(item){ 
    return occurrences[item] === 1; 
  });
}
DRobinson
  • 4,341
  • 19
  • 29