Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,181 questions

56,073 answers

573 users

How to find repeated rows of a matrix in Scala

1 Answer

0 votes
object MatrixDuplicateFinder {
  // Helper function to convert a row to a string for comparison
  def rowToString(row: Array[Int]): String = row.mkString(",")

  // Function to find repeated rows in the matrix
  def findRepeatedRows(matrix: Array[Array[Int]]): Unit = {
    val rowCount = scala.collection.mutable.Map[String, Int]()

    matrix.foreach { row =>
      val pattern = rowToString(row)
      rowCount(pattern) = rowCount.getOrElse(pattern, 0) + 1
    }

    println("Repeated Rows:")
    rowCount.filter(_._2 > 1).foreach { case (pattern, count) =>
      println(s"Row: [$pattern] - Repeated $count times")
    }
  }

  def main(args: Array[String]): Unit = {
    val matrix = Array(
      Array(1, 2, 3),
      Array(4, 5, 6),
      Array(1, 2, 3),
      Array(7, 8, 9),
      Array(4, 5, 6),
      Array(0, 1, 2),
      Array(4, 5, 6)
    )

    findRepeatedRows(matrix)
  }
}


 
/*
run:

Repeated Rows:
Row: [4,5,6] - Repeated 3 times
Row: [1,2,3] - Repeated 2 times

*/

 



answered May 24, 2025 by avibootz
...