2017-08-16 2 views
1

私は6ダイスの配列からトリプレットを除外する関数を書こうとしています。 LodashまたはUnderscoreを使用して簡単に行う方法はありますか?繰り返しがある場合、配列からいくつかの項目を削除するには? (Lodash/Underscore preferred)

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]) // = []

+0

あなたは簡単に考えるかに依存... What'veあなたは、これまで行われていますか。 – Teemu

+0

ヘイジョー - あなたはあなたが探している条件を確認できますか?例3では、配列に2つの '1 'を返しますが、例4と5ではnoneを返します。条件は、3つの数字があるたびにそれらの3つを削除することですが、4つある場合は1を返します。6がある場合は、3と3を削除するので、noneを返します。 –

+0

@ChristopherMesserあなたは正しいです。私は配列内の三つ組を削除したい。 –

答えて

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 { 
 
\t 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)

+0

これは私が探していた方法ですが、実装はロダッシュを使ってもっと短くすることができます。ここでそれを取ります: 'function noTriplets(arr){ var hashMap = _.countBy(arr); return _.flatten(_。keys(hashMap).map(n =>配列(hashMap [n]%3).fill(n))) } –

1

あなたはすべての項目のカウントを使用して無視する項目の数を計算できます。

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; }

1

あなたは値を記録して、前のオブジェクトを使用して新しい配列を生成するためにオブジェクトを使用することができます。

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

1

純粋なJSと私のユニバーサルソリューション。繰返し項目をいくつ削除するかを指定できます。たとえば、ここではnoDoubles,noTripletsおよびnoQuadruplesのメソッドを作成しました。

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]

0

グレート答え!みんなの答えを読んだ後、特にクリストファーメッサーのは、私はlodashベースのバージョンを思い付いた:

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

関連する問題