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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:09:08+00:00 2026-06-15T20:09:08+00:00

99 scala problems has this question: Pack consecutive duplicates of list elements into sublists.

  • 0

99 scala problems has this question:

Pack consecutive duplicates of list elements into sublists.
If a list contains repeated elements they should be placed in separate sublists.

Example:
scala> pack(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e))
res0: List[List[Symbol]] = List(List('a, 'a, 'a, 'a), List('b), List('c, 'c), List('a, 'a), List('d), List('e, 'e, 'e, 'e))

I have an understanding of the tail-recursive way to solve the above given problem. I was wondering if there was a way to accomplish the above using scanLeft where the intermediate result is the list of common elements?

  • 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-15T20:09:10+00:00Added an answer on June 15, 2026 at 8:09 pm

    Here’s a solution using foldLeft:

    def pack[A](l:List[A]): List[List[A]] = l match {
      case head :: tail =>
        tail.foldLeft(List(List(head))) { (collector:List[List[A]], elem:A) =>
          if (collector.head.head == elem)
            (elem +: collector.head) +: collector.tail
          else
            List(elem) +: collector
        }.reverse
      case _ => List.empty
    }
    

    This only works for Lists.
    A better solution would probably use MultiSets, although finding a Scala implementation is difficult.

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

Sidebar

Related Questions

Forgive me if this has already been asked elsewhere. I have a Scala syntax
Problem can be found here: http://rosalind.info/problems/subs/ The question I have has to do with
I suspect this has been asked before, but can't seem to find a question
I know this question has been answered several times before, but this newbie could
I have a simple Scala project that runs without any problems inside Eclipse, however,
I am having problems using the update method of scala.collection.immutable.HashMap.I don't see the reason
I saw this thread: What are the biggest differences between Scala 2.8 and Scala
This is a follow on from my last question relating to plotting timestamps in
This is an SQL efficiency question. A while back I had to write a
Simple Problem: Ever since I switched to Idea 10.5, it has this auto import

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.