Finding sum of elements in Swift array

后端 未结 16 1677
耶瑟儿~
耶瑟儿~ 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:31

    this is my approach about this. however I believe that the best solution is the answer from the user username tbd

    var i = 0 
    var sum = 0
    let example = 0
    for elements in multiples{
        i = i + 1
        sum = multiples[ (i- 1)]
        example = sum + example
    }
    

提交回复
热议问题