search

Home  >  Q&A  >  body text

javascript - Find elements that appear multiple times in a two-dimensional array

var arr=new Array(["A","B"],["C","D"],["E","F"],["A","C"],["D","F"],["E","A"],["A","F"]);
var output_arr=new Array();
//找出出现过三次及以上的元素,写入到output_arr这个一维数组中
//"A"出现了4次,"F"出现了3次
output_arr = ["A","F"];
phpcn_u1582phpcn_u15822753 days ago611

reply all(3)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-19 10:49:27

    Give you a reference- -

    let arr = new Array(["A", "B"], ["C", "D"], ["E", "F"], ["A", "C"], ["D", "F"], ["E", "A"], ["A", "F"]);
    let result = [],list = {},output_arr = [];

    Calculate the number of occurrences of elements and filter out elements that meet the conditions

      for (let [val1, val2] of arr) {
        result.push(val1, val2);
      }
      for (let val of result) {
        list[val] ? list[val] += 1 : list[val] = 1;
        if (list[val] === 3) { //出现3次以上的元素
          output_arr.push(val); //装进数组
        }
      }

    View results

      console.log(output_arr);

    reply
    0
  • 迷茫

    迷茫2017-05-19 10:49:27

    The ES5 solution is given here, and the ES5 compatibility will be better.

    The implemented code is as follows, with specific comments given:

    var arr = new Array(["A","B"],["C","D"],["E","F"],["A","C"],["D","F"],["E","A"],["A","F"]);
    var output_arr = new Array();
    
    // 处理函数
    function filterWord(arr){
        
        // 先定义一个对象
        var tempObject = {};
    
        // 两层for循环,获得数组的每个元素    
        var arrLength = arr.length;
        for(var i = 0; i < arrLength; i++){
    
            var arrItemLength = arr[i].length;
            for(var j = 0; j < arrItemLength; j++){
                
                // 获得数组的元素
                var temp = arr[i][j];
    
                // 判断是否已经存在于对象中
                if(temp in tempObject){
                    // 如果存在,那么计数加一
                    tempObject[temp]++;
                }else{
                    // 如果不存在,就新创建,并且计数为1
                    tempObject[temp] = 1;
                }
            }
        }
    
        // 将计数大于等于3的加入数组
        var resultArr = [];
        for(var item in tempObject){
            if(tempObject[item] >= 3){
                resultArr.push(item);
            }
        }
        return resultArr;
    }
    
    output_arr = filterWord(arr);
    console.log(output_arr);

    Output result:

    ["A", "F"]

    Here are some suggestions. I hope the subject will be familiar with the objects in JavaScript and the methods of traversing and processing objects.
    In this question, the subject can test the result of outputting tempObject. The results are as follows:

    {
        A: 4
        B: 1
        C: 2
        D: 2
        E: 2
        F: 3
    }

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-19 10:49:27

    Give me a solution for ES6:

    let arr = new Array(["A","B"],["C","D"],["E","F"],["A","C"],["D","F"],["E","A"],["A","F"]);
    
    let conatiner = [].concat(...arr).sort();
    let dictionary = [...new Set(conatiner)];
    let retArr = dictionary.filter((val, idx) => (
        (conatiner.lastIndexOf(val) - conatiner.indexOf(val) + 1) >= 3
    ));

    reply
    0
  • Cancelreply