java - Comparing values in a 2-Dimensional Array with each other -


i had computer science exam today. in have write number of different algorithms. of them managed do, 1 of first questions totally threw me off.

a 2 dimensional array used store digits in 3 * 3 grid of sudoku game. grid contained numbers 1 9. of unfamiliar sudoku, allowed use each number once in 3*3 grid. question asked me write checkgrid() algorithm compare values in array each other check if there duplicates. if none found, "success" should output, if there double values, "failure" should output. how can achieve this? don't have clue.

i spent way time in exam trying figure out , barely managed finish paper -.- maybe should listen teachers when tell skip question don't know come later...

there many options, easiest way can think of using set:

public void checkgrid(int[][] grid) {     set<integer> set = new hashset();     (int = 0; < 3; i++) {         (int j = 0; j < 3; j++) {             if (set.contains(grid[i][j])) {                 system.out.println("failure");                 return;             } else {                 set.add(grid[i][j]);             }         }     }     system.out.println("success"); } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -