繁体   English   中英

在Javascript数组中查找特定的连续数字集

[英]Find the specific consecutive set of numbers in a Javascript array

我想在数组中连续发现一个整数。 我找到了一个类似的例子:

const numbers = [0,0,0,296,296,0,0,296,296,296,0,0,0,0,0,293,293,293,293,293,293,293,293];

let chunks = [];

let prev = 0;
numbers.forEach((current) => {
  if ( current - prev != 1 ) chunks.push([]);

  // Now we can add our number to the current chunk!
  chunks[chunks.length - 1].push(current);
  prev = current;
});

chunks.sort((a, b) => b.length - a.length);

console.log('Longest consecutive set:', chunks[0]);
console.log('Size of longest consecutive set:', chunks[0].length);

我只想得到0的最长连续数,如:

result: {
  key: 0
  longestConsecutive: 5
}

有什么好办法吗? 谢谢!

也许你可以做类似以下的事情:

 const numbers = [0,0,0,296,296,0,0,296,296,296,0,0,0,0,0,293,293,293,293,293,293,293,293,91]; function findConsecCount(value) { var startIndex = -1 var result = 0 for(var i = 0; i < numbers.length; i++) { if(numbers[i] === value) { if(startIndex === -1) { startIndex = i } } if(numbers[i] !== value) { if(startIndex !== -1) { result = Math.max(result, i - startIndex) startIndex = -1; } } } if(numbers[numbers.length - 1] === value) { if(startIndex !== -1) { result = Math.max(result, i - startIndex) startIndex = -1; } } return { key : value, longestConsecutive : result } } console.log( findConsecCount(0) ) console.log( findConsecCount(293) ) console.log( findConsecCount(296) ) console.log( findConsecCount(91) ) 

您可以使用Array.prototype.reduce为目标值创建序列长度数组,然后使用Math.max获取该数组中的最大值(这是最长序列的长度):

 const numbers = [0,0,0,296,296,0,0,296,296,296,0,0,0,0,0,293,293,293,293,293,293,293,293]; function max_consecutive ( arr, value ) { return Math.max( ...numbers.reduce( (sequence_lengths, x) => { x === value ? sequence_lengths[0]++ : sequence_lengths.unshift(0); return sequence_lengths; }, [0] ) ); } console.log( max_consecutive( numbers, 0 ) ); console.log( max_consecutive( numbers, 1 ) ); console.log( max_consecutive( numbers, 293 ) ); console.log( max_consecutive( numbers, 296 ) ); 

您还可以使用reduce sort键组合“块”长度,然后为每个键sort它们进行sort

 const numbers = [0, 0, 0, 296, 296, 0, 0, 296, 296, 296, 0, 0, 0, 0, 0, 293, 293, 293, 293, 293, 293, 293, 293]; let consecutiveGroups = numbers.reduce((acc, curr, i, arr) => { if (!(curr in acc)) acc[curr] = []; if (arr[i - 1] != curr) acc[curr].push(1); else acc[curr][acc[curr].length - 1] += 1; return acc; }, {}); Object.values(consecutiveGroups) .forEach(g => g.sort((a, b) => b - a)); console.log(consecutiveGroups); console.log('longest consecutive 0s', consecutiveGroups[0][0]); console.log('longest consecutive 296s', consecutiveGroups[296][0]); 

[使用长度而不是数字数组的更新受到了@Paulpro梦幻般的解决方案的启发]

我觉得这可能是一个简单的while循环是最容易理解的情况,应该很快。 这只是在数组上运行两个索引,跟踪找到的最长序列:

 const numbers = [0, 0, 0, 296, 296, 0, 0, 296, 296, 296, 0, 0, 0, 0, 0, 293, 293, 293, 293, 293, 293, 293, 293]; function longestConsecutive(searchedFor, numbers) { let start = stop = longest = 0 while (start < numbers.length + 1) { if (numbers[start] !== searchedFor) { longest = start - stop > longest ? start - stop : longest stop = start + 1 } start++ } return longest } console.log(longestConsecutive(0, numbers)) console.log(longestConsecutive(296, numbers)) console.log(longestConsecutive(293, numbers)) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM