在列表中组织重复的数字

给定一个数字数组,函数应返回一个数组数组,其中每个子数组都包含特定数字的所有重复项。

子数组的顺序应与它们所包含的数字的第一次出现的顺序相同。鉴于此:

[3, 2, 6, 2, 1, 3]

我怎么能得到这个

[[3, 3], [2, 2], [6], [1]]


GCT1015
浏览 102回答 5
5回答

四季花海

您可以采用一个对象对相同的值进行分组。var array = [3, 2, 6, 2, 1, 3],    result = [],    group = {};for (let value of array) {    if (!group[value]) result.push(group[value] = []);    group[value].push(value);}console.log(result);.as-console-wrapper { max-height: 100% !important; top: 0; }

阿晨1998

我相信它可以写得更短,更简单,这是我的镜头:function group(arr) {  let dubArray = []  arr.forEach((item) => {    let itemArr = []    arr = arr.filter((repeatItem)=>{      if(item === repeatItem) {        itemArr.push(item)        return false      }       return true    })    if(itemArr.length > 0 )      dubArray.push(itemArr)  })  return dubArray}console.log(group([1, 2, 1, 5, 5, 1]))

繁华开满天机

Map&nbsp;对象在此类情况下非常有用。它非常有效,并且迭代条目始终按插入顺序进行。let arr = [3, 2, 6, 2, 1, 3];// form a Map with number as key sub arrays as valuelet aMap = new Map();arr.forEach(num => {&nbsp; if (!aMap.has(num)) { aMap.set(num, []); }&nbsp; let subArr = aMap.get(num);&nbsp; subArr.push(num);&nbsp; aMap.set(num, subArr);});// loop through Map to form array of sub-arrayslet counts = [];aMap.forEach(subArr => {&nbsp; counts.push(subArr);});const result = document.getElementById('result');result.innerHTML = 'arr: ' + JSON.stringify(arr) + '\n';result.innerHTML += 'aMap: {';aMap.forEach((subArr, number) => {&nbsp; result.innerHTML += ` ${number} => ${JSON.stringify(subArr)},`;});result.innerHTML += ` }\n`;result.innerHTML += 'counts: ' + JSON.stringify(counts);<pre id="result"></pre>

当年话下

我做了一个函数,可以做到这一点,称为组。let test = [3, 2, 6, 2, 1, 3];function group(list){&nbsp; &nbsp; let result = [];&nbsp; &nbsp; let used = []&nbsp; &nbsp; for (let i = 0; i<list.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; let sames = [];&nbsp; &nbsp; &nbsp; &nbsp; for (let j = i+1; j<list.length; j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (list[i] == list[j] && !used.includes(list[i])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sames.push(list[j])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (!used.includes(list[i])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sames.push(list[i])&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; used.push(list[i]);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (sames.length > 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.push(sames)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; return result}console.log(group(test))

FFIVE

let arr = [3, 2, 6, 2, 1, 3];let unique_set = []; // storage of first occured unique numberslet result = [];arr.forEach(n => {&nbsp; let index = unique_set.indexOf(n);&nbsp; if( index !== -1 ) return result[index].push(n);&nbsp;&nbsp;&nbsp; unique_set.push(n);&nbsp; result.push( [n] );&nbsp; // the index of number in set, and index of result array will always match});console.log(result);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript