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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:43:35+00:00 2026-05-16T16:43:35+00:00

Assuming: val l1 = List(1,2,3) val l2 = List(2,3,1) I want a method that

  • 0

Assuming:

val l1 = List(1,2,3) 
val l2 = List(2,3,1)

I want a method that confirms that l1 is equal to l2 (as in same contents but different order). Is there an API method on List/Seq to do this?

l1.sameElements(l2)

does not work as it verifies order as well.

I’ve come up with the following:

l1.foldLeft(l1.size == l2.size)(_ && l2.contains(_))

Is there anything more succinct than the above to do this comparison?

  • 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-16T16:43:35+00:00Added an answer on May 16, 2026 at 4:43 pm

    If what you want is “these lists contain the same elements, irrespective of order or repetitions”:

    l1.toSet == l2.toSet

    If what you want is “these lists contain the same elements, and with the same number of repetitions of each”:

    l1.sorted == l2.sorted

    If what you want is “these lists contain the same elements and are the same size, but the number of repetitions of a given element can differ between the two lists”:

    l1.size == l2.size && l1.toSet == l2.toSet

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

Sidebar

Related Questions

Assuming a fluid layout is not an option (since that is a different discussion
Assuming that I have two tables, names and phones , and I want to
Assuming there are 5 items in the settings file ( MySetting1 to MySetting5 ),
Assuming I'm trying to automate the installation of something on windows and I want
Assuming that we have a project named project and modules module1 and module2 ,
Assuming I can telnet to gitorious.org port 9418 (default for git), but not on
Assuming such a query exists, I would greatly appreciate the help. I'm trying to
Assuming you can't use LINQ for whatever reason, is it a better practice to
Assuming network access is sporadic with no central server, what would be the best
Assuming I have only the class name of a generic as a string in

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.