How to find duplicate elements in an array with Scala

1 Answer

0 votes
object FindDuplicates {
  def main(args: Array[String]): Unit = {
    val arr = Array(1, 2, 3, 2, 2, 4, 4, 4, 4, 3, 5, 6, 3)
    
    val duplicates = arr.groupBy(identity).collect { case (x, Array(_, _, _*)) => x }
    
    println(s"Duplicates: ${duplicates.mkString(", ")}")
  }
}
 
 
    
/*
run:
    
Duplicates: 2, 3, 4
    
*/

 



answered Oct 26, 2024 by avibootz
...