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]
あなたは簡単に考えるかに依存... What'veあなたは、これまで行われていますか。 – Teemu
ヘイジョー - あなたはあなたが探している条件を確認できますか?例3では、配列に2つの '1 'を返しますが、例4と5ではnoneを返します。条件は、3つの数字があるたびにそれらの3つを削除することですが、4つある場合は1を返します。6がある場合は、3と3を削除するので、noneを返します。 –
@ChristopherMesserあなたは正しいです。私は配列内の三つ組を削除したい。 –