Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to check if a 3x3 grid is a valid Sudoku grid in Go

1 Answer

0 votes
// Sudoku solution must satisfy all of the following rules:
// Each of the digits 1-9 must occur once in each row.
// Each of the digits 1-9 must occur once in each column.
// Each of the digits 1-9 must occur once in each 3x3 grid.

package main

import (
    "fmt"
)

// isValidSudoku3x3Grid checks if a 3x3 grid is a valid Sudoku subgrid.
func isValidSudoku3x3Grid(grid [][]int) bool {
    if len(grid) != 3 || len(grid[0]) != 3 {
        return false // Ensure it's a 3x3 grid
    }

    seen := make(map[int]bool)
    for _, row := range grid {
        for _, num := range row {
            if num < 1 || num > 9 || seen[num] {
                return false // Invalid if the number is out of range or repeated
            }
            seen[num] = true
        }
    }

    return true // Valid if all numbers 1-9 appear exactly once
}

func main() {
    grid := [][]int{
        {5, 3, 4},
        {6, 7, 2},
        {1, 9, 8},
    }

    if isValidSudoku3x3Grid(grid) {
        fmt.Println("The grid is a valid Sudoku grid!")
    } else {
        fmt.Println("The grid is NOT a valid Sudoku grid!")
    }
}

 
 
/*
run:
 
The grid is a valid Sudoku grid!
 
*/

 



answered May 30, 2025 by avibootz
edited May 30, 2025 by avibootz

Related questions

...