Swift Program to get the flattened 1D array


Swift has various functions like flatmap(), compactmap(), reduce() to flatten an 1D array.

An array is used to store elements of same data type in an order whereas a set is used to store distinct elements of same data type without any definite order.

Method 1: Using flatmap() Function

To flatten 2D array into 1D array we can use flatmap() function. The flatmap() function return an array by concatenating the sub arrays into a single array.

Syntax

func flatMap{_res}

Here, res is a closure which accepts the element of the specified array as its argument and return a flattened array.

Example

Following Swift program to get the flattened 1D array using flatmap() function

import Foundation
import Glibc

// Function to print the flatten array
func fArray(arr: [[Int]]) -> [Int] {
   return arr.flatMap { $0 }
}

let mArray = [[23, 21, 13], [1, 1, 10], [89, 81, 19]]
let outputArray = fArray(arr: mArray)

print("Original Array:", mArray)
print("Flattened Array:", outputArray)

Output

Original Array: [[23, 21, 13], [1, 1, 10], [89, 81, 19]]
Flattened Array: [23, 21, 13, 1, 1, 10, 89, 81, 19]

Here in the above code, we have a 2D array. Now we create a fArray() function which takes 2D array as an input and return a flattened array using flatMap{$0} function.

Method 2: Using compactMap() Function

We can also flatten 2D array into 1D array using compactMap() function. The compactMap() function takes the specified array element as an argument and then transform them and return the result in a non-nil array.

Syntax

func compactMap{_transform}

Here, transform is a closure which accepts the element of the specified array as its argument and return an optional value.

Example

Following Swift program to get the flattened 1D array using contactMap() function

import Foundation
import Glibc

// Function to print the flatten array
func FlatArray(arr: [[Int]]) -> [Int] {
   return arr.compactMap{$0}.reduce([], +)
}

let mArray = [[3, 20, 13], [11, 12, 10], [9, 81, 1]]
let outputArray = FlatArray(arr: mArray)

print("Original Array:", mArray)
print("Flattened Array:", outputArray)

Output

Original Array: [[3, 20, 13], [11, 12, 10], [9, 81, 1]]
Flattened Array: [3, 20, 13, 11, 12, 10, 9, 81, 1]

Here in the above code, we have a 2D array. Now we create a FlatArray() function which takes 2D array as an input and return a flattened array using compactMap{$0}.reduce([], +) function.

Method 3: Using User Defined Function

The can also create a user defined function to flatten 2D array into 1D array. In this function, we run a nested for loop to iterate through each element of the 2D array and then append the element in the 1-D array.

Example

Following Swift program to get the flattened 1D array using a user-defined function.

import Foundation
import Glibc

// Function to print the flatten array
func myArray(arr: [[Int]]) -> [Int] {
   var resultantArray: [Int] = []
   for subArr in arr {
      for ele in subArr {
         resultantArray.append(ele)
      }
   }
   return resultantArray
}

let mArray = [[3, 20, 13, 1, 11], [51, 12, 10, 3, 33]]
let outputArray = myArray(arr: mArray)

print("Original Array:", mArray)
print("Flattened Array:", outputArray)

Output

Original Array: [[3, 20, 13, 1, 11], [51, 12, 10, 3, 33]]
Flattened Array: [3, 20, 13, 1, 11, 51, 12, 10, 3, 33]

Here in the above code, we have a 2-D array. Now we create a function named myArray() to flatten 1-D array. This function takes an array as an argument and then create a 1-D array to store the result and then run a nested for loop to iterate through each element of the given 2-D array and store each element in the resultantarray.

Method 4: Using reduce() Function

To flatten 2D array into 1D array we can use reduce() function. The reduce() function is used to combine the elements of the array according to the given closure.

Syntax

func reduce{_initialRes, _nextRes}

Here, initialRes is used to store initial accumulating value which is further passed to the nextRes at the first time the closure is executed. The nextRes is a closuer that is used to combine accumulating value and an element of the sequence into a new accumulating value, which is further used in the next call of the nextRes closure.

Example

Following Swift program to get the flattened 1D array with the help of reduce() function.

import Foundation
import Glibc

// Function to print the flatten array
func FArray(arr: [[Int]]) -> [Int] {
   return arr.reduce([], +)
}

let mArray = [[3, 20, 13, 1], [11, 12, 10, 3], [9, 81, 1, 3], [2, 33, 1, 22]]
let outputArray = FArray(arr: mArray)

print("Original Array:", mArray)
print("Flattened Array:", outputArray)

Output

Original Array: [[3, 20, 13, 1], [11, 12, 10, 3], [9, 81, 1, 3], [2, 33, 1, 22]]
Flattened Array: [3, 20, 13, 1, 11, 12, 10, 3, 9, 81, 1, 3, 2, 33, 1, 22]

Here in the above code, we have a multi-D array. Now we create a FArray() function which takes multi-D array as an input and return a flattened array using reduce([], +) function.

Conclusion

Using above methods you can easily flatten multi-dimensional array into one-dimensional array. All the above methods can work with any type of array, like float, int , string, etc.

Updated on: 19-Jul-2023

228 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements