Skip to the content

Routes with no repeats

Problem

A bus stop sign saves the number of every bus that went by, in the order they went by: 12, 7, 12, 40, 7. The same route shows up several times, and the sign only wants to show which routes stop there, with no repeats. Write a function that takes a list of numbers and returns a new list with those numbers from smallest to largest and with no repeats. With the example above you return 7, 12 and 40.

With an empty list you return an empty list, and if a single number comes in you return a list with that number.

A hint: once you sort, the repeats end up next to each other, so it is enough to compare each number with the one right before it.

Examples

  • Out of order and repeated

    [12, 7, 12, 40, 7] → [7, 12, 40]

  • No repeats

    [3, 1, 2] → [1, 2, 3]

  • Already sorted

    [1, 1, 2, 3, 3] → [1, 2, 3]

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

You start with this

Python

def no_repeats(numbers):
    pass

JavaScript

function noRepeats(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 →