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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:40:45+00:00 2026-06-14T21:40:45+00:00

I have a Seq and function Int => Int. What I need to achieve

  • 0

I have a Seq and function Int => Int. What I need to achieve is to take from original Seq only thoose elements that would be equal to the maximum of the resulting sequence (the one, I’ll have after applying given function):

def mapper:Int=>Int= x=>x*x
val s= Seq( -2,-2,2,2 )
val themax= s.map(mapper).max
s.filter( mapper(_)==themax)

But this seems wasteful, since it has to map twice (once for the filter, other for the maximum).
Is there a better way to do this? (without using a cycle, hopefully)

EDIT
The code has since been edited; in the original this was the filter line: s.filter( mapper(_)==s.map(mapper).max). As om-nom-nom has pointed out, this evaluates `s.map(mapper).max each (filter) iteration, leading to quadratic complexity.

  • 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-14T21:40:46+00:00Added an answer on June 14, 2026 at 9:40 pm

    Here is a solution that does the mapping only once and using the `foldLeft’ function:

    The principle is to go through the seq and for each mapped element if it is greater than all mapped before then begin a new sequence with it, otherwise if it is equal return the list of all maximums and the new mapped max. Finally if it is less then return the previously computed Seq of maximums.

    def getMaxElems1(s:Seq[Int])(mapper:Int=>Int):Seq[Int] = s.foldLeft(Seq[(Int,Int)]())((res, elem) => {
      val e2 = mapper(elem)
      if(res.isEmpty || e2>res.head._2) 
        Seq((elem,e2)) 
      else if (e2==res.head._2) 
        res++Seq((elem,e2)) 
      else res 
    }).map(_._1) // keep only original elements
    
    // test with your list
    scala> getMaxElems1(s)(mapper)
    res14: Seq[Int] = List(-2, -2, 2, 2)
    
    //test with a list containing also non maximal elements
    scala> getMaxElems1(Seq(-1, 2,0, -2, 1,-2))(mapper)
    res15: Seq[Int] = List(2, -2, -2)
    

    Remark: About complexity

    The algorithm I present above has a complexity of O(N) for a list with N elements. However:

    • the operation of mapping all elements is of complexity O(N)
    • the operation of computing the max is of complexity O(N)
    • the operation of zipping is of complexity O(N)
    • the operation of filtering the list according to the max is also of complexity O(N)
    • the operation of mapping all elements is of complexity O(M), with M the number of final elements

    So, finally the algorithm you presented in your question has the same complexity (quality) than my answer’s one, moreover the solution you present is more clear than mine. So, even if the ‘foldLeft’ is more powerful, for this operation I would recommend your idea, but with zipping original list and computing the map only once (especially if your map is more complicated than a simple square). Here is the solution computed with the help of *scala_newbie* in question/chat/comments.

    def getMaxElems2(s:Seq[Int])(mapper:Int=>Int):Seq[Int] = {
      val mappedS = s.map(mapper) //map done only once 
      val m = mappedS.max         // find the max
      s.zip(mappedS).filter(_._2==themax).unzip._1
    }
    
    // test with your list
    scala> getMaxElems2(s)(mapper)
    res16: Seq[Int] = List(-2, -2, 2, 2)
    
    //test with a list containing also non maximal elements
    scala> getMaxElems2(Seq(-1, 2,0, -2, 1,-2))(mapper)
    res17: Seq[Int] = List(2, -2, -2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following pattern matching case in a scala function: def someFunction(sequences: Iterable[Seq[Int]]):Seq[Int]
So I have a function that I've pulled from a tutorial: let sumOfSquares nums
Say I have a method that turns a (function on two elements) into a
We have a table in our system that would benefit from a numeric column
I have a vector of dates: Date = seq(from=as.POSIXct(2011-01-01 00:00), to=as.POSIXct(2011-12-31 23:00), length=8760) It
I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
I have a function in Oracle that returns a sequence number. When I try
I have Clojure function that takes a sequence of numbers chops it into the
I have a function that at the moment programmed in a functional model and
I have a VB.NET application that, as a part of this function, writes to

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.