Finding sum of elements in Swift array

后端 未结 16 1499
耶瑟儿~
耶瑟儿~ 2020-11-30 18:12

What is the easiest (best) way to find the sum of an array of integers in swift? I have an array called multiples and I would like to know the sum of the multiples.

16条回答
  •  有刺的猬
    2020-11-30 18:48

    Swift 3

    From all the options displayed here, this is the one that worked for me.

    let arr = [6,1,2,3,4,10,11]
    
    
    var sumedArr = arr.reduce(0, { ($0 + $1)})
    print(sumedArr)
    

提交回复
热议问题