object Flatten2DArrayIntoSortedUniqueValues {
// Function that flattens a 2D array, sorts it, removes duplicates,
// and returns a new sorted array with unique values
def flattenSortUnique(array2d: Array[Array[Int]]): Array[Int] = {
// 1. Flatten the 2D array into a 1D array cleanly using flatten
val flat = array2d.flatten
// 2. Sort the flattened array
val sorted = flat.sorted
// 3. Remove duplicates using distinct
val unique = sorted.distinct
unique
}
def main(args: Array[String]): Unit = {
val array2d = Array(
Array(4, 3, 3, 2, 4),
Array(30, 10, 10),
Array(10, 30),
Array(1, 1, 6, 7, 7, 7, 8)
)
val arr = flattenSortUnique(array2d)
// Print results
arr.foreach(n => print(n + " "))
}
}
/*
run
1 2 3 4 6 7 8 10 30
*/