USING MAP FUNCTION
let arrMap = arrOfOpration.map { (result) -> Int in return result * 10 }
let arrMap = arrOfOpration.map { $0 * 10 } //map on int array print(arrMap) // it will print [10, 20, 30, 40, 50, 100, 200, 300, 400, 500]
Suppose we have an array of integers
var arrOfOpration = [1,2,3,4,5,10,20,30,40,50] // array of Int
First we multiply by 10 to every elements using for-in loop :
var newArr: [Int] = [] //for-in loop for value in arrOfOpration { newArr.append(value*10) } print(newArr)
let arrMap = arrOfOpration.map { $0 * 10 } //map on int array print(arrMap) // it will print [10, 20, 30, 40, 50, 100, 200, 300, 400, 500]
-
- First a constant arrOfOpration is defined of type array, and initialised with a random values.
- Then function map is called on the constant arrOfOpration, the function has one argument, which multiply values by 10
- Finally, the result is print.
USING FILTER FUNCTION
A filter is used to loop over an every item in the array and return an array containing only those values which satisfy an include condition. The filter is a very useful operation which allow us to filter the existing elements and filter out based on our requirements.Filter method has a single argument that specifies the include condition. This function takes as argument from the array and return a bool if the item should be included in the result.
var arrOfOpration = [1,2,3,4,5,10,20,30,40,50]
let arrFilter = arrOfOpration.filter{(arrOfOpration) -> Bool in arrOfOpration % 2 != 0 } print(arrFilter) // it will print [1,3,5]
let even = arrOfOpration.filter({ (value:Int) Bool in return value % 2 == 0 }) print(even) // it will print [2, 4, 10, 20, 30, 40, 50]
USING REDUCE FUNCTION
Returns the result of combining the elements of the sequence using the given closure. It loops over every item in an array and combines into one value. It reduces the result from an array of elements to a single most probably a computed value from the collection. -Simply it convert multiple values into one value. Syntax :-func reduce<result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) throws -> Result) rethrows -> Result
OR
let arrReduce = numbers.reduce(0) {$0 + $1}
OR
let reduce = numbers.reduce(0,*)
Let’s understand with an example
Suppose we have an array of integers
var arrOfOpration = [1,2,3,4,5,10,20,30,40,50]
let arrReduce = arrOfOpration.reduce(0, {x,y in x+y }) print(arrReduce) // it will print 165
USING FLATMAP FUNCTION
Flatmap will give us a single set of element and it will remove unnecessary element as well. If we have two arrays within an array then it will combine into a single array. Flatmap resulting returns an array. Syntax :- func Flatmap(_ transform: (Element) throws -> SegmentOfResult) rethrows -> [SegmentOfResult.Element] where SegmentOfResult : Sequence Let’s understand with an examplelet arrOfOprationString = ["SWIFT","Array"]
let arrFlaten = arrOfOprationString.flatMap{return $0} print(arrFlaten) // it will print [“S”, “W”, “I”, “F”, “T”, “A”, “r”, “r”, “a”,”y”]
let arrWithInArr = [[10,25,30],[45,50]] let arrFlaten = arrWithInArr.flatMap{(array)in return array } print(arrFlaten) // it will print [10,25,30,45,50]
CONTAINS ELEMENTS
It will return a value indicating whether the sequence contains an element that satisfies given predicate. Suppose we have two arrays and find the same value in both arrays than is contained is used. Let’s understand with an examplelet arr1 = [1,2,3,4,5] let arr2 = [3,4,5,6,7]
let arrSimilar = arr1.filter(arr2.contains) print(arrSimilar) // it will print [3,4,5]
let arr1 = ["Swift","Array","YES"] let arr2 = ["Swift","Array"] let arrSimilar = arr1.filter(arr2.contains) print(arrSimilar) // it will print [“Swift”,”Array”]
UNION
It will merge two arrays and returns an array. Let’s understand with an examplelet arr1 = [1,2,3,4,5] let arr2 = [3,4,5,6,7]
let arrSet1 = Set(arr1) let arrSet2 = Set(arr2) let arrUnion = Array(arrSet1.union(arrSet2)) print(arrUnion) // it will print [3,1,4,6,7,2,5]
let arr1 = ["Swift","Array","YES"] let arr2 = ["Swift","Array”] let arrSetString1 = Set(arrString1) let arrSetString2 = Set(arrString2) let arrUnionString = Array(arrSetString1.union(arrSetString2)) print(arrUnionString) // it will print [“Swift”,”Array”,”YES”]
INTERSECTION
It will merge two arrays and returns an array. Returns a value that similar in both collection and returns an array Let’s understand with an example :-let arr1 = [1,2,3,4,5] let arr2 = [3,4,5,6,7]
let arrIntersection1 = Set(arr1) let arrIntersection2 = Set(arr2) let arrInter = Array(arrIntersection1.intersection(arrIntersection2)) print(arrInter) // it will print [3,4,5]
let arr1 = ["Swift","Array","YES"] let arr2 = ["Swift","Array"] let arrIntersecStr1 = Set(arrString1) let arrIntersecStr2 = Set(arrSetString2) let arrInterstring = Array(arrIntersecStr1.intersection(arrIntersecStr2)) print(arrInterstring) // it will print ["Swift", "Array"]
ADDING ELEMENTS
It is similar as contains. Let’s understand with an examplevar arrOfOpration = [1,2,3,4,5,10,20,30,40,50]
arrOfOpration.insert(contentsOf: 100...103, at: 3) // it will print [1,2,3,111,222,333,4,5,10,20,30,40,50]
APPEND
It will add a new element at the end of collection. And it will append a single element to the end of array. Let’s understand with an examplevar arrOfOpration = [1,2,3,4,5,10,20,30,40,50]
arrOfOpration.append(85) // it will print [1,2,3,4,5,10,20,30,40,50,85]
INSERT AT
It will insert a new element at a specific position of existing element. Index must be a valid index of the array. Let’s understand with an examplevar arrOfOpration = [1,2,3,4,5,10,20,30,40,50]
arrOfOpration.insert(100, at: 3) // it will print [1, 2, 3, 100, 4, 5, 10, 20, 30, 40, 50]
SUBSCRIPT ELEMENTS
It will accesses the element at the specific position. Let’s understand with an examplevar arrOfOpration = [1,2,3,4,5,10,20,30,40,50]
print(arrOfOpration[5]) // it will print 10
if let firstNumber = arrOfOpration.first { print(firstNumber) // it will print 1 }
if let lastNumber = arrOfOpration.last { print(lastNumber) // it will print 50 }
let arrRange = arrOfOpration[2 ..< arrOfOpration.endIndex] print(arrRange) // it will print [3,4,5,10,20,30,40,50]
let arrRandom = arrOfOpration.randomElement()! print(arrRandom) // it will print any random number of the collection
REPLACE ELEMENTS
It will replace the new element to the existing element in collection. Let’s understand with an examplevar arrOfOpration = [1,2,3,4,5,10,20,30,40,50]
arrOfOpration[3] = 19 print(arrOfOpration) // it will print [1,2,3,19,5,10,20,30,40,50]
arrOfOpration.replaceSubrange(Range(1...2), with: [7,7]) print(arrOfOpration) // it will print [1,7,7,19,5,10,20,30,40,50]
REMOVE ELEMENTS
It will remove and return the element at the specific position. Let’s understand with an examplevar arrOfOpration = [1,2,3,4,5,10,20,30,40,50]
let arrRemove = arrOfOpration.remove(at: 5) print(arrRemove) // it will print [1,2,3,4,5,20,30,40,50]
let arrRemoveFirst = arrOfOpration.removeFirst() print(arrRemoveFirst) // it will print [2,3,4,5,10,20,30,40,50]
let arrRemoveLast = arrOfOpration.removeLast() print(arrRemoveLast) // it will print [1,2,3,4,5,10,20,30,40]
arrOfOpration.removeSubrange(1..<4) print(arrOfOpration) // it will print [1,10,20,30,40,50]
REVERSE ELEMENTS
It returns the elements of an array in reverse order. Let’s understand with an examplevar arrOfOpration = [1,2,3,4,5,10,20,30,40,50]
let arrReverse = Array(arrOfOpration.reversed()) print(arrReverse) // it will print [50,40,30,20,10,5,4,3,2,1]
COUNT ELEMENTS
It returns the number of elements present in collection. Let’s understand with an examplevar arrOfOpration = [1,2,3,4,5,10,20,30,40,50]
print(arrOfOpration.count) // it will print 10