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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:33:48+00:00 2026-06-11T21:33:48+00:00

I used a map to scan an input file and produce a word count.

  • 0

I used a map to scan an input file and produce a word count.
the map keys are words and the values are occurrences of the word.
I converted the map to a list and sorted ascending by value.
Thus, I have a list of 2 tuples, with the least frequently occurring word in the first position of the list, and the most frequently occurring word in the last position of the list.

I know I can easily get the 2tuple in the last position of the list with myList.last and then accessing the second element of the result with the myTuple._2

Can it be done in a single line of code?
I want to make it as concise as I can.

  • 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-11T21:33:49+00:00Added an answer on June 11, 2026 at 9:33 pm

    Do you mean:

    val muList = List(("aardvark", 1), ("cow", 5), ("dog", 9))
    muList.last._2  // 9
    

    This will get the last element of muList and then the second item in the tuple found there.

    But you don’t need to convert to a List and sort and all that. You can just get the max-count element from the Map, and it will be much faster:

    val mu = Map("aardvark" -> 1, "dog" -> 9, "cow" -> 5)
    val highestCountPair = mu.maxBy(_._2)   // ("dog",9)
    highestCountPair._2                     // 9
    

    Or, if you don’t care about the word at all:

    mu.map(_._2).max  // 9
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used the Tiled map editor to make a XML file which has items
Quick background I have a concurrent map I used to cache some values that
I used a map<CString, vector<double>> structure to store the mapping of file name to
I have a class with a std::map used as a container. I want to
I used a Google map in my site, and the container div which contains
I used hadoop to run map-reduce applications on our cluster. The jobs take around
I've created a Map App using the JXMapKit and used the waypoint class to
I was used to putting id's in the URL to map to an item
All I have used osmdroid and i want to download map zip files from
I want to Display Google Map into my wordpress site. I have used below

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.