我需要找到包含不超过两个不同数字的数组的最大切片。
这是我的数组[1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8]
我对此的思考过程是找到不重复的数字并在新数组中返回它们的索引。
这是我到目前为止所拥有的:
function goThroughInteger(number) {
var array = [];
//iterate the array and check if number is not repeated
number.filter(function (element, index, number) {
if(element != number[index-1] && element != number[index+1]) {
array.push(index);
return element;
}
})
console.log(array);
}
goThroughInteger([1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8]);
我不确定下一步该去哪里,我正在努力理解这个问题 - 找到包含不超过两个不同数字的最大切片 - 这让我感到困惑。