Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

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)
        }
    }

}
Comment

average in array swift

let sumArray = intArray.reduce(0, +)

let avgArrayValue = sumArray / intArray.count
Comment

PREVIOUS NEXT
Code Example
Swift :: swift inheritance 
Swift :: symfony swiftmailer 
Swift :: swift remove value dictionary 
Swift :: swiftlint 
Swift :: hello world in swift 
Swift :: declaring optionals swift 
Swift :: Swift Nested function 
Swift :: swift get file size from url 
Swift :: swift string format double 
Swift :: swift extension Array where type 
Swift :: swift 5 change message color of alert 
Swift :: float vs double in swift 
Swift :: how to change background color swift 
Swift :: two variable sum 
Swift :: Modifying Value Types from Method Swift 
Swift :: Swift Floating-point Literals 
Swift :: how to debug before app launch swift 
Swift :: Save and Load Data From Keychain ios swift 
Swift :: store multiple items in one core data record 
Swift :: list header swiftui 
Swift :: swift constants 
Swift :: swiftui slide menu 
Swift :: selenium lfor loops 
Swift :: how to unwrap arrays with optional value in swift 
Swift :: Swift Modify the Elements of an Array 
Swift :: load plist swift 
Ruby :: rails create database only in test 
Ruby :: rspec check array without order 
Ruby :: hello world in ruby 
Ruby :: create rails project with postgres 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =