在寻找在javascript中的多个阵列对称差/独特的元素 [英] finding symmetric difference/unique elements in multiple arrays in javascript

查看:131
本文介绍了在寻找在javascript中的多个阵列对称差/独特的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我努力解决这个问题。如何创建一个JavaScript函数,采用任何数量的数组作为参数,然后返回,只有出现在阵列中的一个元素的数组。出现在多个阵列中的所有项目都被删除。越来越行不通了一个解决方案,怀疑我没有接近它以正确的方式,难倒了!

编辑:另一个问题地址消除在一个阵列中重复的值,我需要比较不同的阵列x个,并返回未阵列之间重复的值。所以([5,6,7],[5,8,9])返回[6,7,8,9]。

 函数符号(参数){
  变种ANS = [];
  对于(VAR I = 0; I<与arguments.length;我++){
    VAR tempArr =参数[I] .filter(功能(EL){
      VAR filtTrue = FALSE;
       对于(VAR J = 0; J<与arguments.length; J ++){
         如果(Array.isArray(参数[J])及和放大器;参数[J] ==参数[I]!){
           如果(参数[J] .indexOf(EL)=== -1){
             filtTrue = TRUE;
              }}
        }
           返回filtTrue;
          });
           ANS = ans.concat(tempArr);
  }    返回答;
  }


解决方案

下面是做到这一点的方法之一。这里的想法是您创建一个映射保持在阵列中的所有项目的计数。然后,循环每个阵列,查找每个值的地图,如果找到,就增加计数。如果没有找到,你设置的数量为1。然后,当所有的阵列完成后,你收集有1计数的任何项目。

您没有具体说明,如果某件物品似乎比在同一阵列中再次做什么,而不是在任何其他阵列。这第一个解决方案将不包括该项目(因为它检测到重复)。它可以适用(一点点更多的复杂性),使该项目,如果这是一个问题(见下文,落实第二code座)。

 函数符号(/ *通过一个或多个阵列这里* /){
    变种ANS = [],碳纳米管= {};    //统计数组中的所有项目
    对于(VAR I = 0; I<与arguments.length;我++){
        参数[I] .forEach(函数(项目){
            如果(cnts.hasOwnProperty(项目)){
                //增加CNT
                碳纳米管++ [项目] .CNT;
            }其他{
                // initalize CNT和价值
                碳纳米管[项目] = {CNT:1,VAL:项目};
            }
        });
    }
    对于(碳纳米管VAR项){
        如果(cnts.hasOwnProperty(项目)及和放大器;碳纳米管[项目] .CNT === 1){
            ans.push(碳纳米管[项目] .VAL);
        }
    }    返回答;
}


如果您想以包括present不止一次在单个阵列中,而不是在任何其他阵列present项目,那么你可以使用这个稍微复杂一点的适应:

 函数符号(/ *通过一个或多个阵列这里* /){
    VAR ANS = [],碳纳米管= {},currentMap;    //统计数组中的所有项目
    对于(VAR I = 0; I<与arguments.length;我++){
        currentMap = {};
        参数[I] .forEach(函数(项目){
            //如果我们还没有这个数组中计入本项目
            如果(!currentMap.hasOwnProperty(项目)){
                如果(cnts.hasOwnProperty(项目)){
                    //增加CNT
                    碳纳米管++ [项目] .CNT;
                }其他{
                    // initalize CNT和价值
                    碳纳米管[项目] = {CNT:1,VAL:项目};
                }
            }
            //保持whethere我们已经在这个数组中计入本项目跟踪
            currentMap [项目] =真;
        });
    }
    //输出具有为1的CNT的所有项目
    对于(碳纳米管VAR项){
        如果(cnts.hasOwnProperty(项目)及和放大器;碳纳米管[项目] .CNT === 1){
            ans.push(碳纳米管[项目] .VAL);
        }
    }    返回答;
}

工作演示: http://jsfiddle.net/jfriend00/bete5k3n/

Hi I'm struggling to solve this problem. How to create a javascript function that takes any number of arrays as arguments, then returns an array of elements that only appear in one of the arrays. All items that appear in multiple arrays are removed. Getting nowhere with a solution, suspect I'm not approaching it in the right way, stumped!

Edit: the other question addresses eliminating duplicate values in one array, I need to compare x number of separate arrays and return the values that aren't duplicated between arrays. So ([5,6,7],[5,8,9]) returns [6,7,8,9].

function sym(args) {
  var ans = [];


  for(var i =0;i<arguments.length;i++){
    var tempArr = arguments[i].filter(function(el){
      var filtTrue = false;
       for(var j = 0;j<arguments.length;j++){
         if(Array.isArray(arguments[j]) && arguments[j] !== arguments[i]){
           if(arguments[j].indexOf(el) === -1){
             filtTrue = true;
              }}
        }
           return filtTrue;
          });
           ans = ans.concat(tempArr);
  }



    return ans;
  }

解决方案

Here's one way to do it. The idea here is that you create a map for keeping counts of all the items in the array. You then cycle through each array, look up each value in the map and, if found, you increment its count. If not found, you set the count to 1. Then, when done with all the arrays, you collect any items that have a count of 1.

You weren't specific about what to do if an item appears more than once in the same array, but not in any other array. This first solution will not include that item (since it detects duplicates). It could be adapted (with a little more complexity) to allow that item if that was an issue (see 2nd code block below for that implementation).

function sym(/* pass one or more arrays here */) {
    var ans = [], cnts = {};

    //count all items in the array
    for (var i = 0; i < arguments.length; i++){
        arguments[i].forEach(function(item) {
            if (cnts.hasOwnProperty(item)) {
                // increase cnt
                ++cnts[item].cnt;
            } else {
                // initalize cnt and value
                cnts[item] = {cnt: 1, val: item};
            }
        });
    }
    for (var item in cnts) {
        if (cnts.hasOwnProperty(item) && cnts[item].cnt === 1) {
            ans.push(cnts[item].val);
        }
    }

    return ans;
}


If you want to include items that are present more than once in a single array, but not present in any other array, then you can use this slightly more complicated adaptation:

function sym(/* pass one or more arrays here */) {
    var ans = [], cnts = {}, currentMap;

    //count all items in the array
    for (var i = 0; i < arguments.length; i++){
        currentMap = {};
        arguments[i].forEach(function(item) {
            // if we haven't already counted this item in this array
            if (!currentMap.hasOwnProperty(item)) {
                if (cnts.hasOwnProperty(item)) {
                    // increase cnt
                    ++cnts[item].cnt;
                } else {
                    // initalize cnt and value
                    cnts[item] = {cnt: 1, val: item};
                }
            }
            // keep track of whethere we've already counted this item in this array
            currentMap[item] = true;
        });
    }
    // output all items that have a cnt of 1
    for (var item in cnts) {
        if (cnts.hasOwnProperty(item) && cnts[item].cnt === 1) {
            ans.push(cnts[item].val);
        }
    }

    return ans;
}

Working demo: http://jsfiddle.net/jfriend00/bete5k3n/

这篇关于在寻找在javascript中的多个阵列对称差/独特的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆