Search code examples
javascriptarraysalgorithmcompareindexof

Algorithm to guess which array element has its index (position) changed inside the same array


I've been looking for a while at this thread but all i could find there as results of comparaison between two arrays are which elements were added or removed.

What i need is to just guess which element has moved from an index to another.

Example: let's take the following array:

let array1 = ['A', 'B', 'C', 'D', 'E', 'F'];

The 'E' element will move from the 4th index to the 2nd index and we wil get :

 let array2 = ['A', 'B', 'E', 'C', 'D', 'F'];

I need a function that returns which element has changed , its new index in the new array2 and its old index in the array1;

I've made such as algo in the past which roughly (from what i remember now) consists of looking for the first different element between both arrays then to check the sequence behind it to guess if the diff element found is itself the moved one or the one which took its place.

So before rewritting it , i wished i could find one ready to use ;)


Solution

  • I am not sure what the full requirements are, but this will detect forward and backwards movement as well as character swaps. It will work on any order, but its prediction on what changed gets less accurate the more changes that occur in a row. Here is a sample that uses a dictionary:

    const array1 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];
    const array2 = ['A', 'B', 'E', 'C', 'D', 'F', 'H', 'I', 'G', 'K' , 'J'];
    let globalOffset = 0;
    let currentSuspect = "";
    var dict = new Object();
    array1.forEach((char, index) => {
      dict[char] = index;
    });
    array2.forEach((char, index) => {
      dict[char] = dict[char] - index;
    });
    
    let offset = 0;
    let prevValue = 0;
    Object.entries(dict).forEach((entry, index) => {
      const [key, value] = entry;
      
      switch(true){
        case offset === 0 && value < -1:
          console.log(`The character ${key} had its index moved forward by ${Math.abs(value)}! \n New index: ${index + Math.abs(value)} - Old index: ${index}`);
          break;
        case offset < 0 && value > 1:
          console.log(`The character ${key} had its index moved backwards by ${value}! \n New index: ${index + offset} - Old index: ${index}`);
          break;
        case prevValue === -1 && offset === -1 && value === 1:
          console.log(`The characters ${key} and ${array2[index]} were swapped!`);
          break;
      }
      prevValue = value;
      offset += value;
    });