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 8964159
In Process

The Archive Base Latest Questions

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

This question has been asked in one or the other way on SO but

  • 0

This question has been asked in one or the other way on SO but not like this. I just came over a very basic issue where I was looking for a statisfying solution 🙂
I got a list of objects which have two integer properties. Now I want to find the max value of both properties of all object in the list.
I came up with three solutions:

First approach:

int max = Math.Max(list.Max(elem => elem.Nr), list.Max(elem => elem.OtherNr));

Second approach:

public int Max(List<Thing> list)
{
  int maxNr = 0;

  foreach (var elem in list)
  {
    if (elem.Nr > maxNr)
      maxNr = elem.Nr;
    if (elem.OtherNr > maxNr)
      maxNr = elem.OtherNr;
  }

  return maxNr;
}

A third approach would be to do the sorting by both attribute and then just take the first entry and get the one or the other property.

I would like to find the fastest way to do this. So of all approaches I like the second one the post (from the performace point of view). Even though the first one is shorter you have to go through the list twice.

Any other solutions?

  • 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-15T16:31:27+00:00Added an answer on June 15, 2026 at 4:31 pm

    If you do

    int max = list.Max(elem => Math.Max(elem.Nr, elem.OtherNr));
    

    it’s still a single-liner but only iterates through the list once. I’d take the single-linedness over the probable slight reduction in efficiency from writing it out by hand.

    (Also, don’t you need a cast from double to int somewhere in there?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looks like this question has been asked thousand times already, but each person's configuration
I know this question has been asked before, but no one has given any
I'm sure some variation of this question has been asked before but all other,
It looks like this question has been asked before, here and in other places
I know this type of question has been asked before, but I could not
I know this question has been asked countless times before, but the other threads
I hope that this question has not been asked elsewhere, but I'm having difficulty
I feel like this one has been asked before, but I'm unable to find
This question has been asked before but i still don't understand it fully so
This question has been asked in a C++ context but I'm curious about Java.

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.