简体   繁体   English

在数组中查找链接的递增值

[英]Finding chained, incremented values in array

I'm trying to find all values in an array that would form a chain of incremented values - all referencing back to a certain starting value. 我试图在一个数组中找到所有值,这些值将形成一连串的增量值-所有这些都引用回到某个起始值。 Increments can go both "up" and "down". 增量可以“上升”和“下降”。

array = [10, 2, 3, 5, 9, 11]

Starting with the number 2 should return: 以数字2开头应返回:

[2, 3]

Starting with the number 10 should return: 从数字10应返回:

[9, 10, 11]

There are of course plenty of inefficient ways of doing this, but I'm asking this here because doing this efficiently is important for my case and I'm such a JS newbie. 当然,有很多效率低下的方法可以做到这一点,但是我在这里问这个问题,因为有效地做到这一点对我的情况很重要,而且我是JS新手。

You can use Array.prototype.includes() to check if a number exists in an array. 您可以使用Array.prototype.includes()检查数组中是否存在数字。 If the number is before the base reference add it using unshift , if after add it using push : 如果该数字在基本引用之前,请使用unshift进行添加;如果在数字之后,则使用push进行添加:

 var array = [10, 2, 3, 5, 9, 11]; function findChain(array, num) { if(!array.includes(num)) { return []; } const result = [num]; let before = num - 1; let after = num + 1; while(array.includes(before)) { result.unshift(before--); } while(array.includes(after)) { result.push(after++); } return result; } console.log('Ref 2 -', findChain(array, 2)); console.log('Ref 5 -', findChain(array, 5)); console.log('Ref 10 -', findChain(array, 10)); console.log('Ref 20 -', findChain(array, 20)); 

A quick solution : 快速解决方案:

var array = [10, 2, 3, 5, 9, 11, 14, 89, 12, 8];

var trouver = nombre => {
  var result = [];
  if (array.indexOf(nombre) !== -1) result.push(nombre);
  else return result;
  for(var chiffre = nombre+1; array.indexOf(chiffre) !== -1; chiffre++) result.push(chiffre);
  for(var chiffre = nombre-1; array.indexOf(chiffre) !== -1; chiffre--) result.push(chiffre);
  return result.sort((a,b) => a-b);
}

console.log(trouver(9)); //[ 8, 9, 10, 11, 12 ]

Another solution could be a double chained list for it. 另一个解决方案可能是为其提供双链列表。

 function getValues(array, value) { var object = Object.create(null), result, o; array.forEach(function (a) { object[a] = object[a] || { value: a, pre: object[a - 1] || null, succ: object[a + 1] || null }; if (object[a - 1]) { object[a - 1].succ = object[a]; } if (object[a + 1]) { object[a + 1].pre = object[a]; } }); o = object[value]; if (o) { result = []; while (o.pre) { o = o.pre; } while (o.succ) { result.push(o.value); o = o.succ; } result.push(o.value); } return result; } var array = [10, 2, 3, 5, 9, 11, 14, 89, 12, 8]; console.log(getValues(array, 2)); console.log(getValues(array, 10)); console.log(getValues(array, 42)); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Try this approach 试试这个方法

  1. Sort the array first 首先对数组进行排序
  2. Iterate array items one by one, keep pushing the start-counter if current-item value is not bigger than last-item by 1 , else reset the start-counter to current-index . 迭代阵列项目一个接一个,不断推动启动计数器 ,如果当前项值是1上年项不是做大,否则复位start-counter 当前指数

For Example : 例如 :

  var arr = [10, 2, 3, 5, 9, 11]; function getAllSequences(arr) { arr.sort(function(a, b) { return a - b }); var startIndex = 0; var endIndex = 0; var lastItem = 0; var chains = []; arr.forEach(function(item, index) { if (index > 0) { if ((item - lastItem) > 1) { extractChain(chains, arr, startIndex, endIndex); startIndex = index; } else { endIndex = index; if (index == arr.length - 1) { extractChain(chains, arr, startIndex, endIndex); } } } lastItem = item; }); return chains; } console.log(getAllSequences(arr)); function extractChain(chains, arr, startIndex, endIndex) { var value = arr.slice(startIndex, endIndex + 1); if (value.length > 0) { chains.push(value); } } 

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

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