Swift Matrix Sum

Finding sum of elements in Swift array

This is the easiest/shortest method I can find.

Swift 3 and Swift 4:

let multiples = [...]
let sum = multiples.reduce(0, +)
print("Sum of Array is : ", sum)

Swift 2:

let multiples = [...]
sum = multiples.reduce(0, combine: +)

Some more info:

This uses Array's reduce method (documentation here), which allows you to "reduce a collection of elements down to a single value by recursively applying the provided closure". We give it 0 as the initial value, and then, essentially, the closure { $0 + $1 }. Of course, we can simplify that to a single plus sign, because that's how Swift rolls.

How to find the summation of all elements in 2d array in Swift?

One way is to use joined() to flatten the array and then reduce to sum it:

let my2dArray = [[01, 02, 03, 04],
[05, 06, 07, 08],
[09, 10, 11, 12],
[13, 14, 15, 16]]

let result = my2dArray.joined().reduce(0, +)

print(result) // 136

Note that my2dArray.joined() doesn't create another array, but instead it creates a FlattenBidirectionalCollection<Array<Array<Int>>> which allows sequential access to the items, both forwards and backwards, but it does not allocate new storage. You can of course do Array(my2dArray.joined()) if you wish to see how it looks in array format.

Swift sum numbers in array adding from specific element

You can either use dropFirst(_:) or use a range as the index.

If it's not a fixed number, you could first use firstIndex(of:) to determine the index where you want to start.

numberArray.dropFirst(3).reduce(0, +)
numberArray[3...].reduce(0, +)

sum a diagonal array

You can try

let arr1 = [[1,2,3],[1,2,3],[1,2,3]]
var sum = 0
arr1.indices.forEach { sum += arr1[$0][$0] }

arr1.indices.forEach { sum += arr1[$0][arr1.count - 1 - $0] } // not compiled 

Sum an array of arrays by position resulting in one array in Swift

There may be several ways like HOF to deal with this situation but if you are new to it like me , you can do it like this :

Considering all the subArrays to have same number of element :

let array = [[1, 2, 3, 4], [5, 2, 7, 0] , [1, 7, 9, 4]]

var finalArray = [Int]()
for i in 0..<(array.first?.count ?? 0) {
var aElement = 0
array.forEach { aArray in
aElement += aArray[i]
}
finalArray.append(aElement)
}

//Final array should look like this at this point : [7, 11, 19, 8]

Calculate sum of every 5 elements in array of Integer in Swift iOS

Here is my solution in playgroud:

//: Playground - noun: a place where people can play

import UIKit

var arr = [1,1,1,1,1,2,2,2,2,2,3,3,3,3,3]

let chunkSize = 5
let chunks = stride(from: 0, to: arr.count, by: chunkSize).map {
Array(arr[$0..<min($0 + chunkSize, arr.count)])
}

print(chunks)

var summ = chunks.map { $0.reduce(0, {$0 + $1}) }

print(summ)

OUTPUT:

[[1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3]]

[5, 10, 15]

Take a look at:
Finding sum of elements in Swift array

Swift Int array every 3 elements sum

You can iterate your collection indices, add a guard to return zero in case it is less than 2 otherwise return the sum of the actual value + the last two values:

let numbers = [1,2,3,4,5,6,7,8,9,10,11,12]

let result = numbers.indices.map { index -> Int in
guard index > 1 else { return 0 }
return numbers[index-2...index].reduce(0,+)
}

result // [0, 0, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33]

or simply

let result = numbers.indices.map { $0 < 2 ? 0 : numbers[$0-2...$0].reduce(0,+) }

How can I get the sum for values in a dictionary in swift?

var dictionary:[String:Double] = [:]

dictionary = ["07/03/21": 331, "08/03/21": 854, "09/03/21": -80]
let sum = dictionary.compactMap {$0.value}.reduce(0, +)
print(sum)

Sample Image



Related Topics



Leave a reply



Submit