Answers for "average in array swift"

0

average in array swift

extension Array where Element: BinaryInteger {

    /// The average value of all the items in the array
    var average: Double {
        if self.isEmpty {
            return 0.0
        } else {
            let sum = self.reduce(0, +)
            return Double(sum) / Double(self.count)
        }
    }

}

extension Array where Element: BinaryFloatingPoint {

    /// The average value of all the items in the array
    var average: Double {
        if self.isEmpty {
            return 0.0
        } else {
            let sum = self.reduce(0, +)
            return Double(sum) / Double(self.count)
        }
    }

}
Posted by: Guest on April-27-2022
0

average in array swift

let sumArray = intArray.reduce(0, +)

let avgArrayValue = sumArray / intArray.count
Posted by: Guest on April-27-2022

Code answers related to "Swift"

Browse Popular Code Answers by Language