function solution(board, moves) {
let answer = 0;
let stack = [];
const n = board.length;
moves.forEach(m => {
for (let i = 0; i < n; i++) {
let doll = board[i][m - 1];
if (doll !== 0) {
if (doll === stack[stack.length - 1]) {
stack.pop();
board[i][m - 1] = 0;
answer += 2;
} else {
stack.push(doll);
board[i][m - 1] = 0;
}
break;
}
}
})
return answer;
}
https://programmers.co.kr/learn/courses/30/lessons/64061?language=javascript