How to print the distinct elements of a list in Scala

1 Answer

0 votes
object UniqueElementsApp extends App {
  val lst = List(3, 5, 9, 1, 7, 8, 1, 9, 0, 3, 9)

  val uniqueElements = lst.filter(item => lst.count(_ == item) == 1)

  println(uniqueElements)
}
 
 
  
/*
run:
 
List(5, 7, 8, 0)
 
*/

 



answered Jul 13, 2025 by avibootz
...