Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
when comparing 2 arrays of strings, determine which elements have moved index

I am trying to find what elements have been moved when comparing two arrays of strings. I want to find the elements that where explicitly moved and not moved as a side effect of other values being moved within the array. Please see example below:

const a = [1, 2, 3, 4, 5, 6];
const b = [1, 5, 6, 2, 3, 4];

findMovedElems(a, b) // returns [5, 6] even though [2, 3, 4,] have changed index too - that was caused as a side effect of 5, 6 being moved.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I didn't tested for others situations (like b with numbers not in a, duplicated numbers, etc.), but works on your example.

    var a = [1, 2, 3, 4, 5, 6, 7, 8];
    var b = [1, 5, 6, 2, 3, 4, 7, 8];

    function findMovedElems(a, b) {
      let result = [{"status" : 'OK', "arr" : []}]
      // navigate over a and b
      for (let i = 0, j = 0; i < a.length && j < b.length; i++, j++) {
        if (a[i] === b[j]) {
          result[result.length - 1].arr.push(a[i]);
          continue;
        }
        result.push({"status" : 'MOVED', "arr" : [] });
        // search for current b char on a
        for(let i2 = i ; i2 < a.length; i2++) {
          if (a[i2] !== b[j]) {
            continue;
          }
          if (i2 === j) {
              // a and b are synchonized again. Reset i and return to main loop
              result[result.length - 1].status = 'OK';
              j--;// warning: decrementing loop variable
              i = j;// warning: changing loop variable
              break;
          }
          // found move. Read from a and b when is equals
          for (let i3 = i2; i3 < a.length && j < b.length; i3++, j++/* warning: incrementing other loop variable*/) {
              if (a[i3] !== b[j]) {
                break;
              }
              result[result.length - 1].arr.push(a[i3]);
          }//for_i3
          // Go back, because new array possition was readed, and should be read again on main loop
          i--;// warning: decrementing loop variable
          j--;// warning: decrementing loop variable
          if (i === j) {
              result.push({"status" : 'OK', "arr" : [] });
          } else {
              result.push({"status" : 'MOVED_SIDE_EFFECT', "arr" : [] });
          }
          break;
        }//for_i2
      }//for_i
      return result;
    }

    console.log(JSON.stringify(findMovedElems(a, b)));

output:

[{"status":"OK","arr":[1]},{"status":"MOVED","arr":[5,6]},{"status":"MOVED_SIDE_EFFECT","arr":[2,3,4]},{"status":"OK","arr":[7,8]}]
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda