Skip to the content

Sold only once

Problem

A stationery shop writes down the code of every notebook sold today, in the order they went out: 3, 8, 3, 5, 9, 8. Codes 3 and 8 were sold twice; codes 5 and 9, only once.

Write a function that takes a list of numbers and returns the sum of the ones that appear exactly once in the whole list. With the list above you return 14, which is 5 + 9.

Watch out for the trap: this is not about adding each number once. The 3 and the 8 add nothing at all, because they are repeated: they are left out completely.

If every number is repeated, or if the list comes in empty, you return 0. The numbers can be negative.

Hint: when you sort the list, the copies of the same number end up side by side. So a number appears exactly once if it is different from the one before it and from the one after it.

Examples

  • Two repeated

    [3, 8, 3, 5, 9, 8] → 14

  • Nothing is repeated

    [4, 1, 7] → 12

  • One shows up three times

    [1, 2, 3, 1, 1, 4, 5, 6] → 20

  • A long, shuffled list

    [1, 10, 9, 4, 2, 10, 10, 45, 4] → 57

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def unique_total(numbers):
    pass

JavaScript

function uniqueTotal(numbers) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n log n) challenges

See all challenges →