Discover numeric sequence in javascript array

Asked

Viewed 367 times

0

Hello!

given the following array:

let array = [3,6,9,10,11,12,16,19]

I need to take the sequential values of this array, that is, only the numbers 9 10 11 12 and save in another array. how would do this?

thank you!

  • If the entry is [3, 6, 9, 10, 11, 12, 16, 19, 20, 21, 22] what should be returned?

  • All sequences must be returned, but this I need to do, is in an object that contains day and year that I use in a calendar in React Active. Let’s say the logic is this: in my calendar if it is marked 3 6 and 9 he marks the days with his description on a list. if it’s a sequence or a range 10 11 12 13 14, I need to know that I have a sequence that goes from 10 to 14 and add the list as only one item that has the same description, I don’t need to create those days because their description is the same.

2 answers

2


An alternative is to use a stack to store successive numbers. It works that way:

  • For each element of the entrance look to the next neighbor:
    • if the element is consecutive to Smash and look at the next neighbor repeating this step successively if necessary.
    • if the element is not consecutive download the stack into the result and point to the last verified element.

let arr = [3, 6, 9, 10, 11, 12, 16, 19, 20, 21, 22, 25];
let resultado = [];

//Para cada elementos da entrada...
for (let i = 0; i < arr.length; i++) {
  let pilha = [arr[i]];                        //inicializa a pilha com o elemento.
  let j = i + 1;                               //Aponta o primeiro candidato a consecutivo.
  //Para cada consecutivo...
  for (; arr[j] == pilha[pilha.length - 1] + 1; j++)
    pilha.push(arr[j]);                        //Empilha o consecutivo.
  if (pilha.length > 1) resultado.push(pilha); //Se a pilha possui mais de um elemento a descarrega no resultado.
  i = j - 1;                                   //Aponta para o ultimo elemento pesquisado.
}


console.log(resultado);

In the comment you presented another requirement to also display individually numbers that do not form lists. To do this simply remove the stack size comparison before composing the result.

let arr = [3, 6, 9, 10, 11, 12, 16, 19, 20, 21, 22, 25];
let resultado = [];

for (let i = 0; i < arr.length; i++) {
  let pilha = [arr[i]];
  let j = i + 1;
  for (; arr[j] == pilha[pilha.length - 1] + 1; j++)
    pilha.push(arr[j]);
  resultado.push(pilha); //Descarrega a pilha no resultado sem fazer comparações de tamanho.
  i = j - 1;
}


console.log(resultado);

0

did so, it works.

let teste = [3, 6, 9, 10, 11, 12, 16, 19];

filtro = [];
anterior = '';
lock = 0;
for (x = 0; x < teste.length; x++) {
  atual = teste[x];
  if (anterior == '') {
    filtro[filtro.length] = atual;
    anterior = atual;
  } else if (atual == anterior + 1) {
    filtro[filtro.length] = atual;
    lock = 1;
    anterior = atual;
  } else if (lock) {
    break;
  } else {
    filtro = [];
    anterior = '';
  }
}
console.log(filtro);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.