Scala Programming: Add Each Element N Times To A Given List Of ...
Maybe your like
Scala Programming: Add each element n times to a given list of integers
Last update on June 12 2025 12:57:28 (UTC/GMT +8 hours)
Write a Scala program to add each element n times to a given list of integers.
Sample Solution:
Scala Code:
object Scala_List { def n_times_elements[A](o_list:List[A], n: Int):List[A] = { o_list flatMap { element => List.fill(n)(element) } } def main(args: Array[String]): Unit = { val nums = List(1,2,3,3,4,5,6,7) println("Original List:") println(nums) val result = n_times_elements(nums, 3) println("Add each element 3 times to a given list of integers:") println(result) } }Sample Output:
Original List: List(1, 2, 3, 3, 4, 5, 6, 7) Add each element 3 times to a given list of integers: List(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7)Go to:
- Scala Programming List Exercises Home ↩
- Scala Exercises Home ↩
PREV : Write a Scala program to triplicate each element immediately next to the given list of integers. NEXT : Write a Scala program to check whether a list contains a sublist.
Scala Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Tag » Add Element List Scala
-
How To Add Elements To A List In Scala (List, ListBuffer)
-
How To Add Elements To A List In Scala
-
Scala Append To List - Linux Hint
-
Appending An Element To The End Of A List In Scala - Stack Overflow
-
11.3. Adding Elements To A List - Scala Cookbook [Book] - O'Reilly
-
How To Append List In Scala With Examples - EduCBA
-
Ajouter Des éléments à La Liste Dans Scala | Delft Stack
-
How To Add Elements To A List In Scala?
-
Scala - Lists - Tutorialspoint
-
Add Element To A List In Scala
-
The List Class | Scala Book
-
Scala Append List To List
-
How To Append A List In Scala | Edureka Community
-
Append To A List Scala Code Example
-
Lists In Scala - DataCamp
-
How To Find The Sum Of Elements Of An Array In Scala
-
List - Scala Documentation
-
Arrays And Lists - Scala Succinctly Ebook - Syncfusion
-
Scala ListBuffer - GeeksforGeeks