/**
 * Given two matrices, find and return a list of common elements.
 *
 * @param matrix1 The first matrix containing integers.
 * @param matrix2 The second matrix containing integers.
 * @return A list of common elements present in both matrices.
 *
 * Example:
 * findCommonElements(listOf(listOf(1, 2, 3), listOf(4, 5, 6), listOf(7, 8, 9)), listOf(listOf(4, 5, 6), listOf(7, 8, 9), listOf(10, 11, 12)))
 * Output: [4, 5, 6, 7, 8, 9]
 */
fun findCommonElements(matrix1: List<List<Int>>, matrix2: List<List<Int>>): List<Int> {

    val commonElements = mutableListOf<Int>()

    // Iterate over rows in matrix1
    for (row1 in matrix1) {
        // Iterate over elements in current row1
        for (element in row1) {
            // Check if element exists in matrix2
            if (matrix2.any { it.contains(element) }) {
                commonElements.add(element)
            }
        }
    }

    return commonElements
}
/**
 * Given two matrices, find and return a list of common elements.
 *
 * @param matrix1 The first matrix containing integers.
 * @param matrix2 The second matrix containing integers.
 * @return A list of common elements present in both matrices.
 *
 * Example:
 * findCommonElements(listOf(listOf(1, 2, 3), listOf(4, 5, 6), listOf(7, 8, 9)), listOf(listOf(4, 5, 6), listOf(7, 8, 9), listOf(10, 11, 12)))
 * Output: [4, 5, 6, 7, 8, 9]
 */
fun findCommonElements(matrix1: List<List<Int>>, matrix2: List<List<Int>>): List<Int> {
/**
 * Given two matrices, find and return a list of common elements.
 *
 * @param matrix1 The first matrix containing integers.
 * @param matrix2 The second matrix containing integers.
 * @return A list of common elements present in both matrices.
 *
 * Example:
 * findCommonElements(listOf(listOf(1, 2, 3), listOf(4, 5, 6), listOf(7, 8, 9)), listOf(listOf(4, 5, 6), listOf(7, 8, 9), listOf(10, 11, 12)))
 * Output: [4, 5, 6, 7, 8, 9]
 */
fun findCommonElements(matrix1: List<List<Int>>, matrix2: List<List<Int>>): List<Int> {

    val commonElements = mutableListOf<Int>()

    // Iterate over rows in matrix1
    for (row1 in matrix1) {
        // Iterate over elements in current row1
        for (element in row1) {
            // Check if element exists in matrix2
            if (matrix2.any { it.contains(element) }) {
                commonElements.add(element)
            }
        }
    }

    return commonElements
}