比较数组中的 2 个或多个连续数字
Compare 2 or more consecutive numbers in an array
我有一个数组:A = [ 2, 2, 0, 0, -1, 1, -1, -1, -1 ]
我希望能够 return 在 2 个或多个连续数字相同的情况下为真。所以在这个数组中,输出数组应该有 5 个真值 [2,2]、[0,0]、[-1,-1,-1]、[-1,-1] 和 [-1,-1 ].
到目前为止,我已经通过 2 个连续的数字在数组上使用了 slice 和 map,并得到了 4 个 true。
const solution = (A) => {
let compare = A.slice(1).map((n,i) => {
return (n === A[i])
})
console.log(compare) // => [ true, false, true, false, false, false, true, true ]
}
const A = [ 2, 2, 0, 0, -1, 1, -1, -1, -1 ];
solution(A);
但是在 [-1,-1,-1] 上得到第五个真让我很困惑。
目前我的比较输出只经过 2 个连续的数字,这就是为什么它给了我 4 个真。我的问题基本上是如何检查 3 个或更多连续数字。
我的决赛会是这样的
compare.filter(word => word === true).length
获得5。
以上 question/answer 是一个很好的起点。但明确你想要什么:
const A = [ 2, 2, 0, 0, -1, 1, -1, -1, -1 ]
let findDuplicates = arr => arr.filter((item, index) => arr.indexOf(item) != index)
const solutionArray = findDuplicates(A).map((e) => true);
console.log(solutionArray); // [true, true, true, true, true]
要连续执行此操作,您的代码将按预期工作:
[2,2] 正确
[2,0] 错误
[0,0] 真
[0,-1] 假
[-1,1] 假
[1,-1] 假
[-1,-1] 真
[-1,-1] 真
这是按顺序比较 2 个连续数字的比较。您希望的第 5 个正确是比较 3 个连续的数字。
您是否正在寻求一种同时比较 (n) 个连续数字的解决方案?
也许真正有用的是 groups:
const findGroups = (arr) => arr.reduce((result, value, index) => {
let windowLength = 2;
while (arr[index + windowLength - 1] === value) {
result.push(arr.slice(index, index + windowLength));
windowLength++;
}
return result;
}, []);
console.log(findGroups([2, 2, 0, 0, -1, 1, -1, -1, -1])); // [[2, 2], [0, 0], [-1, -1], [-1, -1, -1], [-1, -1]]
console.log(findGroups([4, 4, 4, 4])); // [[4, 4], [4, 4, 4], [4, 4, 4, 4], [4, 4], [4, 4, 4], [4, 4]]
这为您提供了一组连续值的数组。如果你只想要5
,那就是.length
,或者你可以直接计算它而不是构建不必要的数组:
const findGroupCount = (arr) => arr.reduce((result, value, index) => {
let windowLength = 2;
while (arr[index + windowLength - 1] === value) {
result++;
windowLength++
}
return result;
}, 0);
console.log(findGroupCount([2, 2, 0, 0, -1, 1, -1, -1, -1])); // 5
console.log(findGroupCount([4, 4, 4, 4])); // 6
我有一个数组:A = [ 2, 2, 0, 0, -1, 1, -1, -1, -1 ]
我希望能够 return 在 2 个或多个连续数字相同的情况下为真。所以在这个数组中,输出数组应该有 5 个真值 [2,2]、[0,0]、[-1,-1,-1]、[-1,-1] 和 [-1,-1 ].
到目前为止,我已经通过 2 个连续的数字在数组上使用了 slice 和 map,并得到了 4 个 true。
const solution = (A) => {
let compare = A.slice(1).map((n,i) => {
return (n === A[i])
})
console.log(compare) // => [ true, false, true, false, false, false, true, true ]
}
const A = [ 2, 2, 0, 0, -1, 1, -1, -1, -1 ];
solution(A);
但是在 [-1,-1,-1] 上得到第五个真让我很困惑。
目前我的比较输出只经过 2 个连续的数字,这就是为什么它给了我 4 个真。我的问题基本上是如何检查 3 个或更多连续数字。
我的决赛会是这样的
compare.filter(word => word === true).length
获得5。
以上 question/answer 是一个很好的起点。但明确你想要什么:
const A = [ 2, 2, 0, 0, -1, 1, -1, -1, -1 ]
let findDuplicates = arr => arr.filter((item, index) => arr.indexOf(item) != index)
const solutionArray = findDuplicates(A).map((e) => true);
console.log(solutionArray); // [true, true, true, true, true]
要连续执行此操作,您的代码将按预期工作:
[2,2] 正确 [2,0] 错误 [0,0] 真 [0,-1] 假 [-1,1] 假 [1,-1] 假 [-1,-1] 真 [-1,-1] 真
这是按顺序比较 2 个连续数字的比较。您希望的第 5 个正确是比较 3 个连续的数字。
您是否正在寻求一种同时比较 (n) 个连续数字的解决方案?
也许真正有用的是 groups:
const findGroups = (arr) => arr.reduce((result, value, index) => {
let windowLength = 2;
while (arr[index + windowLength - 1] === value) {
result.push(arr.slice(index, index + windowLength));
windowLength++;
}
return result;
}, []);
console.log(findGroups([2, 2, 0, 0, -1, 1, -1, -1, -1])); // [[2, 2], [0, 0], [-1, -1], [-1, -1, -1], [-1, -1]]
console.log(findGroups([4, 4, 4, 4])); // [[4, 4], [4, 4, 4], [4, 4, 4, 4], [4, 4], [4, 4, 4], [4, 4]]
这为您提供了一组连续值的数组。如果你只想要5
,那就是.length
,或者你可以直接计算它而不是构建不必要的数组:
const findGroupCount = (arr) => arr.reduce((result, value, index) => {
let windowLength = 2;
while (arr[index + windowLength - 1] === value) {
result++;
windowLength++
}
return result;
}, 0);
console.log(findGroupCount([2, 2, 0, 0, -1, 1, -1, -1, -1])); // 5
console.log(findGroupCount([4, 4, 4, 4])); // 6