Valid Sudoku Visualizer & Step-by-Step Algorithm Solution

Validate a 9x9 Sudoku board checking rows, columns, and 3x3 subgrids.

Category: arrays | Difficulty: Medium

Tags: Hash Set, Matrix, Validation

Valid Sudoku

9x9 Sudoku Board
0
1
2
3
4
5
6
7
8
0
1
2
3
4
5
6
7
8
5
3
·
·
7
·
·
·
·
6
·
·
1
9
5
·
·
·
·
9
8
·
·
·
·
6
·
8
·
·
·
6
·
·
·
3
4
·
·
8
·
3
·
·
1
7
·
·
·
2
·
·
·
6
·
6
·
·
·
·
2
8
·
·
·
·
4
1
9
·
·
5
·
·
·
·
8
·
·
7
9
Active Cell
Active Row / Col / Box
Conflict / Subtracted
Highlighted / Subgrid
Seen Subsets State
(Empty Map)
100%
state
board size9x9
Initialization
1/195
Explanation

Start isValidSudoku algorithm on 9x9 board.

Source Code
1function isValidSudoku(board) {
2 const rows = Array.from({ length: 9 }, () => new Set());
3 const cols = Array.from({ length: 9 }, () => new Set());
4 const boxes = Array.from({ length: 9 }, () => new Set());
5 for (let r = 0; r < 9; r++) {
6 for (let c = 0; c < 9; c++) {
7 const val = board[r][c];
8 if (val === ".") continue;
9 const box = Math.floor(r / 3) * 3 + Math.floor(c / 3);
10 if (rows[r].has(val) || cols[c].has(val) || boxes[box].has(val)) {
11 return false;
12 }
13 rows[r].add(val);
14 cols[c].add(val);
15 boxes[box].add(val);
16 }
17 }
18 return true;
19}