Java solution to check if a Sudoku solution is valid Here's my solution to checking if a Sudoku board is valid. The algorithm checks the following,


Rows add up to 45
Columns add up to 45
Rows have no duplicates
Cols have no duplicates
Every 3X3 sub-grid has no duplicate


Invite comments.

public class Checker {
    private int size;
    private int[][] board;

    public Checker(int size){
        this.size = size;
        board = new int[size][size];
    }

    private boolean rowCheck(){
        int sum = (size*(size+1))/2;
        int count = 0;
        for (int i = 0; i &lt;size ; i++) {
            for (int j = 0; j &lt; size ; j++) {
                count += board[i][j];
            }
            if(count != sum) return false;
            count = 0;
        }
        return true;
    }

    private boolean columnCheck(){
        int sum =0;
        int count = 0;
        for (int i = 0; i &lt;size ; i++) {
            for (int j = 0; j &lt;size ; j++) {
                count += board[j][i];
            }
            if(count != sum) return false;
            count = 0;
        }
        return true;
    }

    private boolean checkRowDuplicates(){
        HashSet&lt;Integer&gt; set = new HashSet&lt;&gt;();
        for (int i = 0; i &lt;size ; i++) {
            for (int j = 0; j &lt; size ; j++) {
               set.add(board[i][j]);
            }
            if(set.size() != size) return false;
            set = null;

        }
        return true;
    }
    private boolean checkColumnDuplicates(){
        HashSet&lt;Integer&gt; set = new HashSet&lt;&gt;();
        for (int i = 0; i &lt;size ; i++) {
            for (int j = 0; j &lt; size ; j++) {
                set.add(board[j][i]);
            }
            if(set.size() != size) return false;
            set = null;

        }
        return true;
    }

    private boolean checkSubBoardDuplicates(){

        return (subBoardTraversal(0,3,0,3) &amp;&amp; subBoardTraversal(0,3,3,6) &amp;&amp; subBoardTraversal(0,3,6,9) &amp;&amp; subBoardTraversal(3,6,0,3)
                &amp;&amp; subBoardTraversal(3,6,3,6) &amp;&amp; subBoardTraversal(3,6,6,9) &amp;&amp; subBoardTraversal(6,9,0,3) &amp;&amp; subBoardTraversal(6,9,3,6)
                &amp;&amp; subBoardTraversal(6,9,6,9));

    }
    private boolean subBoardTraversal(int rowStart, int rowFinish, int colStart, int colFinish){
        HashSet&lt;Integer&gt; set = new HashSet&lt;&gt;();
        for (int i = rowStart; i &lt; rowFinish  ; i++) {
            for (int j =colStart ; j &lt; colFinish; j++) {
                set.add(board[i][j]);
            }
            if(set.size() != 9) return false;

        }
        return true;
    }
    public boolean isValid(){

        return(rowCheck() &amp;&amp; columnCheck() &amp;&amp; checkRowDuplicates() &amp;&amp; checkColumnDuplicates() &amp;&amp; checkSubBoardDuplicates());

    }

    public void trace(){
        for (int i = 0; i &lt; size; i++) {
            for (int j = 0; j &lt; size; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }
}
Your setting of count = 0 should be immediately before the appropriate for loop, not after it, and I would actually call that variable sum (renaming your sum to target) since it's not really a "count" at all, e.g:

private boolean rowCheck(){
    int target = size * (size + 1) / 2;
    for (int i = 0; i &lt;size; i++) {
        int sum = 0;        // declaration moved, too
        for (int j = 0; j &lt; size ; j++) {
            sum += board[i][j];
        }
        if (sum != target) return false;
    }
    return true;
}


The point being that you should be setting the variable in advance of the loop, not resetting it afterwards (that being a pointless operation on the final iteration).

I would probably also move target into a class member, calculated in the constructor.