# Function that flattens a 2D array, sorts it, removes duplicates,
# and returns a new sorted array with unique values
def flatten_sort_unique(array2d)
# 1. Flatten the 2D array into a 1D array
flat = array2d.flatten
# 2. Sort the flattened array
flat.sort!
# 3. Remove duplicates using uniq
unique = flat.uniq
unique
end
array2d = [
[4, 3, 3, 2, 4],
[30, 10, 10],
[10, 30],
[1, 1, 6, 7, 7, 7, 8],
]
arr = flatten_sort_unique(array2d)
# Print results
arr.each do |n|
print "#{n}\t"
end
=begin
run
1 2 3 4 6 7 8 10 30
=end