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

  • Home
  • SEARCH
  • 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 6850269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:06:12+00:00 2026-05-27T01:06:12+00:00

Following up this question I wonder why maxBy of Traversable[T] returns a single value

  • 0

Following up this question I wonder why maxBy of Traversable[T] returns a single value T instead of a sequence of T (list or similar). It looks like a pretty common case. For example (from the previous question):

For the list of students with their grades

List(Student("Mike", "A"), Student("Pete", "B"), Student("Paul", A))"

I want to get

List(Student("Mike", "A"), Student("Paul", A))

Does anybody know about any standard implementation of maxBy, which returns a sequence of found items?

  • 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-05-27T01:06:12+00:00Added an answer on May 27, 2026 at 1:06 am

    There is no single command. The shortest I know of–which will group everything not just the maximum value as an intermediate–is

    xs.groupBy(f).maxBy(_._1)._2
    

    For greater efficiency, folds are good general-purpose tools for finding sums and maxima and various similar things. Basically, any time you need to run over your collection while accumulating some answer, use a fold. In this case,

    (xs.head /: xs.tail) {
      (biggest, next) => if (f(biggest) < f(next)) next else biggest
    }
    

    will perform maxBy(f) if you don’t mind re-evaluating the function twice for each element, while

    ((xs.head, f(xs.head)) /: xs.tail) {
      case (scored, next) =>
        val nextscore = f(next)
        if (scored._2 < nextscore) (next, nextscore)
        else scored
    }._1
    

    will do it with only one evaluation per element. If you want to keep a sequence, you can modify this to

    (Seq(xs.head) /: xs.tail) {
      (bigs, next) =>
        if (f(bigs.head) > f(next)) bigs
        else if (f(bigs.head) < f(next)) Seq(next)
        else bigs :+ next
    }
    

    to keep the list (the corresponding single-evaluation form is left as an exercise to the reader).

    Finally, even the near-maximum efficiency version isn’t all that hard to manage, if you’re willing to use a few mutable variables (hopefully well-hidden in a code block like I have here)

    val result = {
      var bigs = xs.take(0).toList
      var bestSoFar = f(xs.head)
      xs.foreach{ x =>
        if (bigs.isEmpty) bigs = x :: bigs
        else {
          val fx = f(x)
          if (fx > bestSoFar) {
            bestSoFar = fx
            bigs = List(x)
          }
          else if (fx == bestSoFar) bigs = x :: bigs
        }
      }
      bigs
    }
    

    (this will return in reverse order, incidentally).

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

Sidebar

Related Questions

Following up to this question , it seems like Select-Object sets its input to
This question led me to wonder the following: What are the 'Gotchas' associated with
Following this question and answer , I still have a bit trouble in the
Following this question, it seems that it is possible to open a file from
Following this question , I use this function to convert arrays to objects, function
Following up this question , I have a further problem - I have two
Following on from this question , I am interested in finding out how you
following on from this question (Developing to an interface with TDD), I'm still having
Following on from this question...I'm trying to unit test the following scenario: I have
Following on from this question... What to do when you’ve really screwed up the

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.