The first one that never repeats
- O(n²) · Very hard
- Full plan
- Python
- JavaScript
- strings
- loops
Problem
Write a function that takes a text and returns the first character that never repeats, that is, the one that shows up a single time in the whole text.
In bubble the answer is u: the b shows up three times, so it does not count, and the u shows up only once.
Watch out for one trap: it is not the first character you had not seen before, it is the one that shows up nowhere else in the text, neither before nor after. In ababc the answer is c, because the a and the b come back later on.
If no character fits, you return the empty text "". With an empty text you also return "".
A space counts as one more character.
Hint: for each character you can walk the whole text counting how many times it shows up. The first one that shows up a single time is the answer.
Examples
Skips the one that repeats
"bubble" → "u"
The first one already works
"melon" → "m"
All the way to the last letter
"ababc" → "c"
A single letter
"z" → "z"
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def first_unique(text):
passJavaScript
function firstUnique(text) {
}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.