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

时间:2017-08-16 03:51:05

标签: javascript node.js underscore.js lodash

我正在尝试编写一个从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]) // = []

5 个答案:

答案 0 :(得分:2)

它有点粗糙和肮脏,但它并不要求你提前知道你的三胞胎。在noTriplets()中 - 我创建一个快速hashMap,然后循环遍历该对象。循环逻辑处理三元组件。



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)
&#13;
&#13;
&#13;

答案 1 :(得分:1)

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

&#13;
&#13;
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])); // []
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您可以使用对象记录值,然后使用上一个对象生成新数组。

&#13;
&#13;
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])); // []
&#13;
&#13;
&#13;

答案 3 :(得分:1)

我使用纯JS的通用解决方案。您可以指定应删除多少重复项。例如,此处创建了noDoublesnoTripletsnoQuadruples方法。

&#13;
&#13;
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]
&#13;
&#13;
&#13;

答案 4 :(得分:0)

很棒的答案!在阅读了所有人的答案后,特别是克里斯托弗·梅塞尔,我想出了一个基于lodash的版本:

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