Home Software Engineering Sum of Array Singles in Python

Sum of Array Singles in Python

Sum of Array Singles in Python


The problem

You might be given an array of numbers wherein two numbers happen as soon as and the remaining happen solely twice. Your job is to return the sum of the numbers that happen solely as soon as.

For instance, repeats([4,5,7,5,4,8]) = 15 as a result of solely the numbers 7 and 8 happen as soon as, and their sum is 15. Each different quantity happens twice.

The answer in Python code

Choice 1:

def repeats(arr):
    depend = []
    for i in arr:
        if i not in depend:
            depend.take away(i)
    return sum(depend)

Choice 2:

def repeats(arr):
    return sum([x for x in arr if arr.count(x) == 1])

Choice 3:

repeats=lambda a:2*sum(set(a))-sum(a)

Check instances to validate our answer

check.it("Fundamental checks")
check.assert_equals(repeats([9, 10, 19, 13, 19, 13]),19)
check.assert_equals(repeats([16, 0, 11, 4, 8, 16, 0, 11]),12)
check.assert_equals(repeats([5, 17, 18, 11, 13, 18, 11, 13]),22)
check.assert_equals(repeats([5, 10, 19, 13, 10, 13]),24)



Please enter your comment!
Please enter your name here