Element search in 2-d array if its below in same next row and next element of same row is 1
This function get the current element in given matrix If current element have value 1 and next column have 1 Then again current element also 1 in next row in same location it should be return.
by Sunny SM
JavaScript
/**
* This function get the current element in given matrix
* If current element have value 1 and next column have 1
* Then again current element also 1 in next row in same location it should be return.
*/
const getElements = (data=[]) => {
const result = [];
if(data && data.length) {
for (let row = 0; row < (data.length - 1); row++) {
for(let col = 0; col < data[row].length; col++) {
if((data[row][col] + data[row][col+1]) === 2 && (data[row+1][col] + data[row+1][col]) === 2) {
console.log(`Element Row: ${row} Column ${col}`);
result.push(data[row][col]);
}
}
}
}
return result;
}
const matrix = [
[1,1,0,1,0,0,1,1],
[1,0,0,1,0,1,1,1],
[0,1,1,1,0,0,1,0],
[1,0,1,1,1,1,0,0]
];
console.log('Result: ', getElements(matrix));