Simple Array Sum hackerank solution in Go and Python

Given an array of integers, find the sum of its elements.

For example, if the array ar= [1,2,3], 1+2+3 so return 6.




Solution:

As per problem statement which we see above is that we need to return sum of given array.

Using python we can simply call inbuilt list sum function, for example:

x = [1,2,3,4]

sum(x) = 10

 

Python3 Solution:


def simpleArraySum(ar):
    return sum(ar)

Go Solution:


func simpleArraySum(ar []int32) int32 {
    var sum int32
    for _, val := range ar {
        sum += val
    }
    return sum
}


Post a Comment

0 Comments