admin管理员组

文章数量:1389352

I am trying to find the difference between two arrays, by finding out which element was moved. I know that one element exactly will be moved and that the order will be maintained for the rest of the list, but am unable to figure out how to find this.

Example:

A: 1 2 3 4 5 6

B: 2 3 4 5 1 6

All of the elements exist in both lists, but how do I find out that the element 1 moved from index 0 to index 4?

My basic approach that I took but is not working is:

//Original array
var a = [1, 2, 3, 4, 5, 6];

//New array
var b = [2, 3, 4, 5, 1, 6];

for(var i=0; i < a.length; i++) {
    if(a[i] != b[i] && a[i+1] != b[i]) {
        console.log(b[i] + " moved");
    }
}

I have fixed by code to print out b[i] instead of a[i], but it is not working in all cases such as:

A: 1, 2, 3, 4

B: 1, 4, 2, 3

I am trying to find the difference between two arrays, by finding out which element was moved. I know that one element exactly will be moved and that the order will be maintained for the rest of the list, but am unable to figure out how to find this.

Example:

A: 1 2 3 4 5 6

B: 2 3 4 5 1 6

All of the elements exist in both lists, but how do I find out that the element 1 moved from index 0 to index 4?

My basic approach that I took but is not working is:

//Original array
var a = [1, 2, 3, 4, 5, 6];

//New array
var b = [2, 3, 4, 5, 1, 6];

for(var i=0; i < a.length; i++) {
    if(a[i] != b[i] && a[i+1] != b[i]) {
        console.log(b[i] + " moved");
    }
}

I have fixed by code to print out b[i] instead of a[i], but it is not working in all cases such as:

A: 1, 2, 3, 4

B: 1, 4, 2, 3

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Mar 30, 2014 at 6:02 FlipperFlipper 2,6073 gold badges25 silver badges32 bronze badges 8
  • define "not working". How does it fail? It looks good to me. – John Dvorak Commented Mar 30, 2014 at 6:21
  • @JanDvorak That code tells me that 5 moved, when the answer should be 1. – Flipper Commented Mar 30, 2014 at 6:22
  • @Smash ... what are you trying to say? – John Dvorak Commented Mar 30, 2014 at 6:22
  • 2 @Flipper sounds like you should be printing b[i], then – John Dvorak Commented Mar 30, 2014 at 6:23
  • @JanDvorak Ah you are right! I just figured it out and was ing back to this page to answer my own question, but I can accept your answer if you write one? – Flipper Commented Mar 30, 2014 at 6:24
 |  Show 3 more ments

4 Answers 4

Reset to default 7

The problem is with the second condition in your if statement. In your example, when element a[0] has moved, a[0+1] === b[0], so the if clause evaluates to false.

Try instead,

var idx = 0;
var len = a.length;
while ((a[idx] === b[idx] || a[idx] === b[idx+1]) && idx < len) {
    idx++;
}
console.log('Element a[' + idx + ']=' + a[idx] + ' moved.');

basically, if I understand correctly, an element moving means it is deleted and inserted somewhere else. so you first find the first point where there was a deletion/insertion:

function whichMoved(a, b) {
      for(var i=0; i < a.length; i++) {
        if (a[i] != b[i]) {

now, if it was a deletion, then the element has been moved forward, meaning, inserted in a greater index in b, and all elements between the indices are shifted to the left, meaning the the next element has moved one place to backward:

if(a[i+1] == b[i]) {
    console.log(a[i] + " moved forward");
    break;
}

otherwise, the element was moved backward:

else {
    console.log(b[i] + " moved backward")
    break;
}

the whole thing:

//Original array
var a = [1, 2, 3, 4, 5, 6];

//testing
whichMoved(a, [2,3,4,5,1,6]); //prints 1 moved forward
whichMoved(a, [5,1,2,3,4,6]); //prints 5 moved backward
function whichMoved(a, b) {
  for(var i=0; i < a.length; i++) {
    if (a[i] != b[i]) {
      if(a[i+1] == b[i]) {
        console.log(a[i] + " moved forward");
        break;
      } else {
        console.log(b[i] + " moved backward")
        break;
      }
    }
  }
}

You can use jQuery .inArray() it will return the index, starting at 0 and returns -1 if not found:

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

//New array
var b = [2, 3, 4, 5, 1, 6];

for(i=0; i < a.length; i++) {
    var j = $.inArray(a[i], b);

    if(i != j){
        console.log(a[i], "moved to index "+j);
    }else{
        console.log(a[i], "not moved");
    } 
}

See this jsfiddle: http://jsfiddle/Rdzj4/

Edited- probably not needed, but I hate to leave a wrong answer.

Here I look at the distance each item is from its original index,

and figure the one that is most out of order is the mover-

This assumes in [2,1,3,4,5,6] it is the two that moved, not the 1,

and in [1,2, 3, 4, 6, 5] it is the 6, not the 5.

function whoMoved(a, b){
    var max= 0, min= a.length, dist, 
    order= b.map(function(itm, i){
        dist= i-a.indexOf(itm);
        if(dist<min) min= dist;
        if(dist>max) max= dist;
        return dist;
    });
    if(Math.abs(min)>= max) max= min;
    return b[order.indexOf(max)];
}

//test

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

var b= [1, 6, 2, 3, 4, 5];//6 to left
var c= [1, 3, 4, 2, 5, 6];//2 to to right
var d= [3, 1, 2, 4, 5, 6];//3 to left
var e= [2, 3, 4, 5, 1, 6];//1 to right

[whoMoved(a, b), whoMoved(a, c), whoMoved(a, d),whoMoved(a, e)];

/*  returned value: (Array) [6,2,3,1] */

本文标签: javascriptHow to determine which element was moved in an arrayStack Overflow