Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check in 2d array if four elements in row and column are the same?

I'm trying to get a nested forEach loop, that find a pair of four in a two dimensional array.

Here a example how my array looks like:

[0, 0, 0, 0, 0],
[0, 2, 0, 0, 0],
[0, 1, 2, 0, 0],
[0, 1, 2, 2, 0],
[1, 2, 1, 1, 2],

It should ignore the 0 and only find horizontal, vertical and diagonal pairs of four of entries with '1' or '2'.

Does anybody have any suggestion?


1 Answers

This is my stab at it using only basic for loops and iteration. I'm sure this is not nearly as efficient as @Redu's example.

Each type of set is a separate function. The output is in the form of coordinates, showing where in the matrix the set is located, where it begins, and where it ends. Obviously the output could be a Boolean or whatever else you want it to be. I've just formatted the output this way to prove that it works.

I've left the filtering part to the user, in this case I have specifically allowed 0's (where your spec says to ignore them) simply because your example input data would then never return a set for the horizontal or diagonal calls. The example below does filter out any sets that contain less than 4 elements.

This example assumes that the rows are all the same length. If the rows are not the same length, more logic would be required.

const Matrix = function(matrix) { this.matrix = matrix; }

Matrix.prototype.getHorizontalSequences = function() {
  const matrix = this.matrix, sets = [];

  for(let i = 0; i < matrix.length; ++i) {
    const row = matrix[i];

    for(let j = 0; j < row.length; ++j) {
      const start = j;
      let k = j + 1;

      for(; k < row.length; ++k) {
        if(row[j] !== row[k]) break;
      }

      sets.push({ row: i, val: row[j], start: start, end: k - 1 });
      j = k - 1;
    }
  }
  
  return sets;
};

Matrix.prototype.getVerticalSequences = function() {
  const matrix = this.matrix, sets = [];

  for(let i = 0; i < matrix[0].length; ++i) {
    for(let j = 0; j < matrix.length; ++j) {
      const start = j;
      let k = j + 1;

      for(; k < matrix.length; ++k) {
        if(matrix[j][i] !== matrix[k][i]) break;
      }

      sets.push({ col: i, val: matrix[j][i], start: start, end: k - 1 });
      j = k - 1;
    }
  }
  
  return sets;
};

Matrix.prototype.getDiagonalSequences = function() {
  const matrix = this.matrix, sets = [];

  for(let i = 0; i < matrix[0].length; ++i) {
    for(let j = i; j < matrix.length; ++j) {
      const start = j;
      let k = j + 1;

      for(; k < matrix.length; ++k) {
        if(matrix[j][i] !== (matrix[j + k] || [])[i + k]) break;
      }

      sets.push({ col: i, val: matrix[j][i], start: start, end: k });
      j = k - 1;
    }
  }
  
  return sets;
};

let matrix = new Matrix([
  [0, 0, 0, 0, 0],
  [0, 2, 0, 0, 0],
  [0, 1, 2, 0, 0],
  [0, 1, 2, 2, 0],
  [1, 2, 1, 1, 2]
])

console.log(matrix.getHorizontalSequences().filter(e => (e.end + 1) - e.start >= 4));
console.log(matrix.getVerticalSequences().filter(e => (e.end + 1) - e.start >= 4));
console.log(matrix.getDiagonalSequences().filter(e => (e.end + 1) - e.start >= 4));
.as-console-wrapper { max-height: 100% !important; }

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!