Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
Kotlin - Check collection contains another collection exactly

I want to be able to check that collection a contains exactly all of the elements of b, but an equality based check is not sufficient; for example:

data class Person(val name: String, val age: Int)

val a = listOf(Person("Alice", 10), Person("Bob", 13))
val b = listOf(Person("Alice", 10), Person("Alice", 10))

fun main() {
   println(a.containsAll(b))
}

true

Whilst this is technically true, it's not the result I want, because a only contains one Person("Alice", 10), whereas b contains two of them.

The above example should fail, whilst the below should pass.

data class Person(val name: String, val age: Int)

val a = listOf(Person("Alice", 10), Person("Alice", 10), Person("Bob", 13))
val b = listOf(Person("Alice", 10), Person("Alice", 10))

fun main() {
   println(a.containsAllExact(b))
}

Is there a way to do this?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You could add an extension function for this, something like:

fun List<Person>.containsAllExact(list2: List<Person>): Boolean {
    val occurences1 = this.groupingBy{ it }.eachCount()
    val occurences2 = list2.groupingBy{ it }.eachCount()
    return occurences2.all{
        it.value <= occurences1.getOrDefault(it.key, 0)
    }

}
over 4 years ago · Santiago Trujillo Report

0

One way I can think of is to make a mutable copy of a, and try to remove every element of b, then check if all elements of b can be removed:

println(
    a.toMutableList().let { aCopy ->
        b.all(aCopy::remove)
    }
)

Or as an extension function:

fun <T> Iterable<T>.strictlyContainsAll(other: Iterable<T>) = 
    toMutableList().let { copy -> 
        other.all(copy::remove)
    }
over 4 years ago · Santiago Trujillo Report

0

I think this would do it

println(a.containsAll(b) && (a.size - b.size == (a-b).size))

Edit: it doesn't work because it gives false negatives also. for example with

val a = listOf(Person("Alice", 10), Person("Alice", 10), Person("Bob", 13))
val b = listOf(Person("Alice", 10))
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!