Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

230
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda