# Sum of Array Singles in Python

## The challenge

You are given an array of numbers in which two numbers occur once and the rest occur only twice. Your task is to return the sum of the numbers that occur only once.

For example, `repeats([4,5,7,5,4,8]) = 15` because only the numbers `7` and `8` occur once, and their sum is `15`. Every other number occurs twice.

## The solution in Python code

Option 1:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```def repeats(arr):
count = []
for i in arr:
if i not in count:
count.append(i)
else:
count.remove(i)

return sum(count)
```Code language: Python (python)```

Option 2:

``````def repeats(arr):
return sum([x for x in arr if arr.count(x) == 1])
```Code language: Python (python)```

Option 3:

``````repeats=lambda a:2*sum(set(a))-sum(a)
```Code language: Python (python)```

## Test cases to validate our solution

``````test.it("Basic tests")
test.assert_equals(repeats([4,5,7,5,4,8]),15)
test.assert_equals(repeats([9, 10, 19, 13, 19, 13]),19)
test.assert_equals(repeats([16, 0, 11, 4, 8, 16, 0, 11]),12)
test.assert_equals(repeats([5, 17, 18, 11, 13, 18, 11, 13]),22)
test.assert_equals(repeats([5, 10, 19, 13, 10, 13]),24)
```Code language: Python (python)```
Tags:
Subscribe
Notify of