Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8928803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:31:53+00:00 2026-06-15T08:31:53+00:00

I have a 2-dimensional array which has rows of different lengths. I want to

  • 0

I have a 2-dimensional array which has rows of different lengths. I want to write a method that returns a new array which consists of the maximum elements of columns. If this was a simple n x m array, it would be easy but since the rows are variable length, I can’t come up with a solution to account for the different number of elements in columns.

For example, the array looks like this:

int[][] test = { { 0, 1, 4, 5, 6, 8 }, 
                 { 4, 5, 8, 3, 9 },
                 { 3, 6, 2 } 
               };

The expected result would then be:

int[] result =  {4, 6, 8, 5, 9, 8};

I have got the code to find the maximum elements of rows but I’m out of ideas how to adjust it for the columns.

int[] result = new int[m.length];

      for (int x = 0; x < m.length; x++) {
         result[x] = 0;
         for (int y = 0; y < m[x].length; y++) {
            if (result[x] < m[x][y]) {
               result[x] = m[x][y];
            } 
         } 
      } 

Any help would be appreciated

EDIT: I realized now that the first thing to do is finding the row with maximum number of elements since that defines the size of the new array. From there.. should probably take the elements of a row and compare them with the elements at the same position in the new array. And do this with every row. Then it doesn’t matter how short the other rows are. Am I on the right way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:31:54+00:00Added an answer on June 15, 2026 at 8:31 am

    First you want to find the length of the largest row.

    Then, similarly to your algorithm, but you want to make sure not to get out of bounds exception. That’s it:

    int maxcol = 0;
    for(int i = 0; i < test.length; i++)
        if(test[i].length > maxcol)
            maxcol = test[i].length;
    
    
    int[] result = new int[maxcol];
    
    for (int j = 0; j < maxcol; j++)
        for (int i = 0; i < test.length; i++)
            if (test[i].length > j && result[j] < test[i][j])
                result[j] = test[i][j];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-dimensional array, which basically consists of one sub-array for each year.
Ok I have a mult-dimensional array which has the following structure... 0 => array
I have a method which returns a array of ByteArray: public byte[][] Draw(ImageFormat imageFormat,
i am using visual studio 2010. i have a three dimensional structure array which,
I have this 4 Dimensional array to store String values which are used to
I have a 2-dimensional jagged array (though it's always rectangular), which I initialize using
Suppose I have some pointer, which I want to reinterpret as static dimension array
I have an 2 dimensional array. I write 3 values into the array during
I have a function which must return one dimensional associate array, like $user_info[$index]=value where
I have a 2-dimensional array of objects (predominantly, but not exclusively strings) that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.