Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!