character
character

Reputation: 310

Find max value of each column in 2D array

I was wondering while using java.

There are two-dimensional arrays as shown below.

int[][] test = {{3, 9, 3, 5}, {4, 19, 4, 9}, {2, 10, 5, 6}};

I want to find the max value in each row in a two-dimensional array, and I wonder how to code it.

I want to result is

int[] answer = {4, 19, 5, 9}

Simply add a comment, I want to extract the largest value.

[0][0] = 3;
[1][0] = 4;
[2][0] = 2;

[0][1] = 9;
[1][1] = 19;
[2][1] = 10;

[0][2] = 3;
[1][2] = 4;
[2][2] = 5;

[0][3] = 5;
[1][3] = 9;
[2][3] = 6;


So max value is 4, 19, 5, 9

I tried this method but this method is arr[i][j] compares all elements in a two-dimensional array and finds only max in the comparison.

for (int i = 0; i < test.length; i++) {
    int max = test[i][0];
    for (int j = 0; j < test[i].length; j++)      
        if (test[i][j] > max)
            max = test[i][j]
    }

Upvotes: 3

Views: 8600

Answers (2)

xingbin
xingbin

Reputation: 28269

You can init the result with the first row, then iterate on this matrix, if the current element test[row][column] is greater than result[column], reassign result[column]:

public static void main(String[] args) {
    int[][] test = {{3, 9, 3, 5}, {4, 19, 4, 9}, {2, 10, 5, 6}};
    int[] result = test[0];
    for (int row = 1; row < test.length; row++) {
        for (int column = 0; column < test[0].length; column++) {
            if (test[row][column] > result[column]) {
                result[column] = test[row][column];
            }
        }
    }
    for (int number : result) {
        System.out.print(number + " "); // 4 19 5 9 
    }
}

Upvotes: 3

vincrichaud
vincrichaud

Reputation: 2208

According to your exemple, the "big" loop is going through the columns and the "small" loop through the lines. Sou you'll have something like that :

for(int j=0; j<test[0].length; j++) { //loop through columns
    for(int i=0; i<test.length; i++) { //loop through lines
    }
}

Then you need a result variable that will be an array : int[] result = new int[test[0].length];

Then you need to have a variable to store the max number of a column. This variable will be re-initialise at every "big" loop since you want to re-determine the max.

int[] result = new int[test[0].length]; //Array to store the result
for(int j=0; j<test[0].length; j++) { //loop through columns
    int max = 0; //int to store the max of the i column
    for(int i=0; i<test.length; i++) { //loop through lines
    }
}

Then for every line check if the number is bigger than max. If it is replace max by the new value.

if(test[i][j] > max) { //if the number at the column i and line j is bigger than max
    max = test[i][j]; then max becomes this number
}

Finally when you've run through every line of the column, add the found max to the result

result[i] = max; //Add the found max to the result array

This should give you this :

int[] result = new int[test[0].length]; //Array to store the result
for(int j=0; j<test[0].length; j++) { //loop through columns
    int max = 0; //int to store the max of the i column
    for(int i=0; i<test.length; i++) { //loop through lines
        if(test[i][j] > max) { //if the number at the column i and line j is bigger than max
            max = test[i][j]; then max becomes this number
        }
    }
    result[i] = max; //Add the found max to the result array
}
System.out.println(Arrays.toString(result)); //print the result

Upvotes: 2

Related Questions