3

I have 2d array java, I need to look at it and check on the max value, then print it with the count of how many it is in the array

I was trying to do like this but it doesn't work

        int[][] rand = new int[][]{
                {1, 80, 3, 4, 5},
                {13, 199, 80, 8},
                {12, 22, 80, 190}
        };

        int max = rand[0][0];
        int count = 0;
        for (int i = 0; i < rand.length; i++){
            for (int ii = 0; ii < rand[i].length; ii++) {
                if (rand[i][ii] > max) {
                    max = rand[i][ii];
                    count++;
                }

            }
        }
        System.out.println(max + " " + count);

3
  • You are on the right path but you need to reset the count every time you find a value that is larger. This means you also need an if statement that says if the value is equal to the max then increase count Commented Dec 27, 2020 at 7:19
  • So in your code where it says count++ do count=1, and then add an else if statement to increase count when the value is equal to the max Commented Dec 27, 2020 at 7:20
  • 1
    In addition, you may want to handle empty arrays. The statement rand[0][0] will raise an OutOfBoundsException on an empty array. Commented Dec 27, 2020 at 7:26

2 Answers 2

2

You are not handling count correctly:

int max = rand[0][0];
int count = 0
for (int i = 0; i < rand.length; i++){
    for (int ii = 0; ii < rand[i].length; ii++) {
        if (rand[i][ii] > max) {
            max = rand[i][ii];
            count = 1; // a new maximum's first occurrence
        } else if (rand[i][ii] == max) {
            count++; // another occurrence of the current maximum
        }
    }
}
System.out.println(max + " " + count);
  • When you find a new maximum, you should reset count to 1.

  • You should check if the current element is equal to the current maximum, and increment count if that's the case.

Output:

199 1

EDIT:

Fixed the case where the first element is the maximum. It turns out count should be initialized to 0 after all, since the loops re-visit the first element, so we don't want to count it twice if it's the maximum.

Sign up to request clarification or add additional context in comments.

3 Comments

It is incorrect sollution. Try it for int[][] rand = new int[][]{ {200, 80, 199, 199, 5}, {13, 199, 80, 8}, {12, 22, 80, 200} };
@A.Alexander Oh, you are correct. count should be initialized to 0 after all.
@Eran - We posted our answers almost at the same time but when I saw your answer, I didn't even realise that you had done that mistake and in the honour of a great as you, I deleted my answer which didn't have this mistake 😊.
0

You can use streams:

    int max = Arrays.stream(rand)
            .mapToInt(row -> Arrays.stream(row).max().getAsInt())
            .max().getAsInt();

    int count = Arrays.stream(rand)
            .mapToInt(row -> (int) Arrays.stream(row).filter(i-> i==max).count())
            .reduce(Integer::sum).getAsInt();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.