javaarraysmultidimensional-array

Java remove duplicates from 2d array


I need to do this problem:

" Prepare a rectangular array (with random natural positive dimensions) of int i variables fill it with random values from the interval [0,10). Create and display a new board created from input array by removing duplicates from each row (leaving only one occurrence of each value). "

I am not allowed to use any libraries

I tried to change dublicates to a number out of the interva [0,10) "1337" for example, and then delete all "1337" thought it would be easier, but I have out of bounds exeption

Main function:

System.out.println("Zadanie 2");
int size = (int)(Math.random()*10);
int[][] arr = new int[size][size];
for (int i = 0; i < arr.length; i++) {
    for (int j = 0; j < arr[i].length; j++) {
        arr[i][j] = (int)(Math.random()*10);
    }
}
removeDuplicates2D(arr);
System.out.println(Arrays.deepToString(arr));

My removeDuplicates2D function:

public static void removeDuplicates2D(int[][] tab) {
    int column = 0, row = 0;
    while (column != tab.length-1 || row != tab.length-1){
        if (column == 0 && row == 0){
            column++;
            continue;
        }
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if(tab[i][j] == tab[row][column]){
                    tab[row][column] = 1337;  // will change all duplicates to 1337
                }
            }
        }
        if(++column == tab.length-1){
            row++;
            column = 0;
        }
    }
}

Solution

  • For the following 6x8 table

    [4, 3, 2, 5, 2, 4, 3, 0]
    [3, 7, 4, 8, 3, 7, 8, 7]
    [9, 4, 5, 0, 6, 7, 3, 0]
    [0, 2, 3, 1, 5, 3, 7, 7]
    [7, 8, 3, 9, 4, 9, 7, 4]
    [8, 2, 8, 4, 0, 0, 1, 1]
    

    The following would be the result of duplicates removed.

    [4, 3, 2, 5, 0]
    [7, 8]
    [9, 6]
    [1]
    []
    []
    

    Details

    • first, establish a single array to hold dups. The location of the value [0-9) indicates if the value has been seen or not. The value is its own index.
    • then just iterate thru the array via rows and columns.
      • when you first encounter a value, if it's dup position is not 0, it must have been seen, so set the current tab value to 1337.
      • if not 0, then set the dup position to 1 and increment the itemCount. This count will be used to create the new row for first seen values.

    Once the dups for a given row have been found

    • create a replacement row using itemCount
    • then iterate over row adding values that are not 1337
    • then assign that row to the current row position for the table.
    public static void removeDuplicates2D(int[][] tab) {
    
        int[] dups = new int[10];
    
        for (int row = 0; row < tab.length; row++) {
            int itemCount = 0;
            int[] currentRow = tab[row];
            for (int col = 0; col < currentRow.length; col++) {
                int value = currentRow[col];
                if (dups[value] != 0) { // has been seen?
                    currentRow[col] = 1337;
                } else {
                    itemCount++;
                    dups[value] = 1; // indicate value has been seen.
                }
            }
    
            int[] newRow = new int[itemCount];
            for (int k = 0, i = 0; i < currentRow.length; i++) {
                if (currentRow[i] != 1337) {
                    newRow[k++] = currentRow[i];
                }
            }
    
            tab[row] = newRow;
        }
    }