Home > Blockchain >  How to remove a duplicate element from a 2D array?
How to remove a duplicate element from a 2D array?

Time:01-20

I began making a sudoku game in go, and I have made it so that it puts random numbers from 1 to 9 in each cell in a sudoku board. But in sudoku, you can't have a duplicate cell horizontally and vertically from the cell.

I have seen ways of removing duplicates from a regular array, but the sudoku board is a 2D array.

(P.S: please don't include type inferring in the answers, I find it hard to read)

CodePudding user response:

You can use two nested loops to check for duplicates in the rows and columns of the 2D sudoku board.

For example, here's one way you could implement a function to check for duplicates in a row:

func checkRow(board [9][9]int, row int) bool {
    // Create a map to store the occurrences of each number
    m := make(map[int]int)

    for i := 0; i < 9; i   {
        if _, ok := m[board[row][i]]; ok {
            // If the number already exists in the map, return true
            return true
        } else {
            // If the number doesn't exist in the map, add it
            m[board[row][i]] = 1
        }
    }
    // If all numbers in the row are unique, return false
    return false
}

You can use similar logic to check for duplicates in a column using this function:

func checkCol(board [9][9]int, col int) bool {
    m := make(map[int]int)

    for i := 0; i < 9; i   {
        if _, ok := m[board[i][col]]; ok {
            return true
        } else {
            m[board[i][col]] = 1
        }
    }
    return false
}

Then you could check for duplicates in the entire sudoku board by using two nested loops that iterate over all the rows and columns, and calling the checkRow and checkCol functions for each cell:

func checkBoard(board [9][9]int) bool {
    for i := 0; i < 9; i   {
        for j := 0; j < 9; j   {
            if checkRow(board, i) || checkCol(board, j) {
                return true
            }
        }
    }
    return false
}

Please note that this function only checks for duplicates within the rows and columns and not the smaller 3x3 sub-grid which is also a rule in the game of sudoku.

Answer:

Removing duplicates from a 2D array can be done by using nested loops to iterate over the rows and columns of the array, and then checking for duplicates in each row and column.

Here is an example of how you could remove duplicates from a 2D sudoku board in Go:

func removeDuplicates(board [9][9]int) [9][9]int {
    // Create a map to store the occurrences of each number
    m := make(map[int]bool)

    for i := 0; i < 9; i   {
        for j := 0; j < 9; j   {
            // If the number already exists in the map, replace it with a 0
            if m[board[i][j]] {
                board[i][j] = 0
            } else {
                // If the number doesn't exist in the map, add it
                m[board[i][j]] = true
            }
        }
        // Clear the map for the next row
        m = make(map[int]bool)
    }

    for j := 0; j < 9; j   {
        for i := 0; i < 9; i   {
            if m[board[i][j]] {
                board[i][j] = 0
            } else {
                m[board[i][j]] = true
            }
        }
        m = make(map[int]bool)
    }

    return board
}

This function iterates over the entire board twice: first it checks for duplicates in the rows and then in the columns, replacing duplicate values with 0.

Please note that this function only removes duplicates within the rows and columns and not the smaller 3x3 sub-grid which is also a rule in the game of sudoku.

  • Related