我需要找到数组中不超过两个不同数字的最大子序列。
这是我的数组:[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]);
我不确定接下来该怎么做,我很难理解这个问题 - 找到包含不超过两个不同数字的最大切片 - 这让我感到困惑。