Thursday 5 May 2022

Scala Function - Combinations

 Scala Collection - Combinations

combinations creates the list of all the possible combinations from given collection by taking "n" number of element at a time.


object MyBlog {
  def main(args: Array[String]): Unit = {
    combinationExample()
  }

  def combinationExample(): Unit = {
    val input = List(1,2,3)
    val combinations = input.combinations(2)
    println(combinations.toList) //List(List(1, 2), List(1, 3), List(2, 3))
  }
}






No comments:

Post a Comment