有重复的时候如何从数组中删除* some *项? (Lodash / Underscore首选)

我试图编写一个函数,从6个骰子数组中筛选出三元组。 有没有简单的方法来使用Lodash或Underscore?

noTriplets([1,1,1,3,3,5]) // = [3,3,5] noTriplets([1,1,1,1,3,5]) // = [1,3,5] noTriplets([1,1,1,1,1,5]) // = [1,1,5] noTriplets([1,1,1,5,5,5]) // = [] noTriplets([1,1,1,1,1,1]) // = [] 

这有点粗糙和肮脏,但它并不需要你提前知道你的三胞胎。 在noTriplets() – 我创build一个快速的noTriplets() ,然后循环通过该对象。 循环逻辑处理三联件。

 const arrayTestOne = [1,1,1,3,3,5]; const arrayTestTwo = [1,1,1,1,3,3,5]; const arrayTestThree = [1,1,1,3,3,3]; const arrayTestFour = [1,1,1,1,3,3,3,5,5,5,5,5,5,5,5,5,5,5,5,7,7]; const hashMap = (array) => array.reduce((allNums, num) => { if (num in allNums) { allNums[num]++ } else { allNums[num] = 1 } return allNums }, {}) function noTriplets(arr) { let newArr = []; let obj = hashMap(arr); for (var key in obj) { for (let i=0; i < obj[key] % 3; i++) { newArr.push(key) } } console.log(newArr) } noTriplets(arrayTestOne) noTriplets(arrayTestTwo) noTriplets(arrayTestThree) noTriplets(arrayTestFour) 

您可以使用每个项目的计数并计算要忽略的项目数量。

 function noTriplets(array) { var hash = {}; array.forEach(function (a) { hash[a] = hash[a] || { count: 0 }; hash[a].ignore = Math.floor(++hash[a].count / 3) * 3; }); return array.filter(function (a, i) { return --hash[a].ignore < 0; }); } console.log(noTriplets([1, 1, 1, 3, 3, 5])); // [3, 3, 5] console.log(noTriplets([1, 1, 1, 1, 3, 5])); // [1, 3, 5] console.log(noTriplets([1, 1, 1, 1, 1, 5])); // [1, 1, 5] console.log(noTriplets([1, 1, 1, 5, 5, 5])); // [] console.log(noTriplets([1, 1, 1, 1, 1, 1])); // [] 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

您可以使用一个对象来logging这些值,然后使用先前的对象来生成一个新的数组。

 function noTriplets(arr){ var tripletCount = arr.reduce((dice,value) => { dice[value] = dice[value] || { count : 0 }; dice[value].count = (dice[value].count + 1) % 3; return dice; },{}); return Object.keys(tripletCount).reduce((arr,key) => { return arr.concat(new Array(tripletCount[key].count).fill(key)); },[]); } console.log(noTriplets([1, 1, 1, 3, 3, 5])); // [3, 3, 5] console.log(noTriplets([1, 1, 1, 1, 3, 5])); // [1, 3, 5] console.log(noTriplets([1, 1, 1, 1, 1, 5])); // [1, 1, 5] console.log(noTriplets([1, 1, 1, 5, 5, 5])); // [] console.log(noTriplets([1, 1, 1, 1, 1, 1])); // [] 

我用纯JS的通用解决scheme。 您可以指定应该删除多less个重复项目。 例如,这里创build了noDoublesnoTripletsnoQuadruples方法。

 function isArrayWithIdenticalElements(array) { return array.length > 1 && !!array.reduce(function(a, b){ return (a === b) ? a : NaN; }); } function noRepetition(numberOfRepetition, array) { var sliceLength = numberOfRepetition - 1; var pointer = sliceLength; var element = array[pointer]; while (element) { if (isArrayWithIdenticalElements(array.slice(pointer - sliceLength, pointer + 1))) { array.splice(pointer - sliceLength, numberOfRepetition); pointer = pointer - sliceLength; element = array[pointer]; } else { pointer = pointer + 1; element = array[pointer]; } } return array; } var noDoubles = noRepetition.bind(null, 2); var noTriplets = noRepetition.bind(null, 3); var noQuadruples = noRepetition.bind(null, 4); console.log('noTriplets([1,1,1,3,3,5] ==> ', noTriplets([1,1,1,3,3,5])); // = [3,3,5] console.log('noTriplets([1,1,1,1,3,5] ==> ', noTriplets([1,1,1,1,3,5])); // = [1,3,5] console.log('noTriplets([1,1,1,1,1,5] ==> ', noTriplets([1,1,1,1,1,5])); // = [1,1,5] console.log('noTriplets([1,1,1,5,5,5] ==> ', noTriplets([1,1,1,5,5,5])); // = [] console.log('noTriplets([1,1,1,1,1,1] ==> ', noTriplets([1,1,1,1,1,1])); // = [] console.log('noQuadruples([1,1,1,3,3,5] ==> ', noQuadruples([1,1,1,3,3,5])); // = [1,1,1,3,3,5] console.log('noQuadruples([1,1,1,1,3,5] ==> ', noQuadruples([1,1,1,1,3,5])); // = [3,5] console.log('noDoubles([1,1,1,5,5,5] ==> ', noDoubles([1,1,1,5,5,5])); // = [1,5] 

很好的答案! 在阅读了所有人的答案之后,尤其是克里斯托弗·梅塞尔(Christopher Messer)的回答,我提出了一个基于lodash的版本:

 function noTriplets(arr) { var hashMap = _.countBy(arr) var filler = n => Array(hashMap[n] % 3).fill(n) return _.flatten(_.keys(hashMap).map(filler)) }