Home > Back-end >  Javascript) I would like to compare two two-dimensional arrays to delete the duplicate elements
Javascript) I would like to compare two two-dimensional arrays to delete the duplicate elements

Time:07-18

As I have already asked, I would like to ask again because I wanted a mutable method, not an immutable method.
I want to compare the two arrays below to delete the duplicate elements.
After deleting the duplicate elements, I want to delete it from the existing ground array, rather than creating a new array.

I approached this way, but it doesn't seem to work properly.

   const filtered = ground.filter((row, idx) => {
            if (row.join() === deleteBlock.join()) {
              return ground.splice(idx, 1);
            }
          });

let ground = [
  [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1,4,3,2,3,4,5,6,7,8,6,5,5,3,2,3,1],
  [1,2,2,2,2,2,2,2,2,2,3,3,3,3,4,5,1],
  [1,3,3,7,7,7,7,8,8,4,4,4,2,2,3,7,1],
  [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
]

let deleteBlock = [
  [1,4,3,2,3,4,5,6,7,8,6,5,5,3,2,3,1],
  [1,2,2,2,2,2,2,2,2,2,3,3,3,3,4,5,1],
  [1,3,3,7,7,7,7,8,8,4,4,4,2,2,3,7,1]
]

CodePudding user response:

So one issue is that when you splice the ground array you are removing the element and then the .forEach method jumps FORWARD to the next element and now you've skipped over an element. If you use a traditional for loop you can shift the for loop back one every time you remove an element so you do not skip any.

Also, you seem to be comparing all of deleteBlock to each row of ground rather than comparing each row of deleteBlock to each row of ground. So I added an additional forEach inside each iteration of the for loop.

for(let x=0;x<ground.length;x  ){
    deleteBlock.forEach((dBlock)=>{
    //check each row to each row
    if (ground[x].join() === dBlock.join()) {
        console.log(dBlock)
        ground.splice(x, 1);
        //after removing an item shift the for loop back one to avoid skipping
        x--
    }
  })
}

CodePudding user response:

First I made an array, the toDelete array contains the joined strings, as in your solution. After then I made a forEach loop, where I looped through the ground named array, so where is the current element includes the toDelete array, I remove it with splice form the original array of the ground. That is mutable!

And here is the solution:

let ground = [
  [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  [1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3,4,5,1],
  [1,3,3,7,7,7,7,8,8,4,4,4,2,2,3,7,1],
  [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
]

let deleteBlock = [
  [1,4,3,2,3,4,5,6,7,8,6,5,5,3,2,3,1],
  [1,2,2,2,2,2,2,2,2,2,3,3,3,3,4,5,1],
  [1,3,3,7,7,7,7,8,8,4,4,4,2,2,3,7,1]
]

const toDelete = deleteBlock.map(row=>row.join(""));

//console.log(toDelete);

ground.forEach((row, index)=>{
  if (toDelete.includes(row.join(""))) {
    console.log("removed: ", row)
    ground.splice(index, 1);
  }
});

console.log(ground);

CodePudding user response:

/*
* There are many ways to reach this solution
* This is my bet
*/
    let ground = [
      [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,4,3,2,3,4,5,6,7,8,6,5,5,3,2,3,1],
      [1,2,2,2,2,2,2,2,2,2,3,3,3,3,4,5,1],
      [1,3,3,7,7,7,7,8,8,4,4,4,2,2,3,7,1],
      [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    ]

    let deleteBlock = [
      [1,4,3,2,3,4,5,6,7,8,6,5,5,3,2,3,1],
      [1,2,2,2,2,2,2,2,2,2,3,3,3,3,4,5,1],
      [1,3,3,7,7,7,7,8,8,4,4,4,2,2,3,7,1]
    ];

/* 
* I prefer use JSON.stringify to be more confident then using Array.join()

deleteBlock = deleteBlock.map(m=>JSON.stringify(m));
newGround = ground.filter(item=>(!deleteBlock.includes(JSON.stringify(item)))); 
 

console.log(newGround);
*/

/* 
* No Array Copy
* for this, you need to pass to Array.splice() the length of the deleteBlock
*/
deleteBlock = deleteBlock.map(m=>JSON.stringify(m));
ground.forEach((item,i)=>deleteBlock.includes(JSON.stringify(item)) ? ground.splice(i,deleteBlock.length) : null);  

console.log(ground);

  • Related