Good afternoon! Here's our prompt for today.
You're given a string of random alphanumerical characters with a length between 0
and 1000
.
Write a method to return the first character in the string that does not repeat itself later on.

So if we had a string input of "asdfsdafdasfjdfsafnnunl'"
, we can see there are multiple letters that are repeated.
Executing firstNonRepeat('asdfsdafdasfjdfsafnnunl')
should return 'j'
since it's the first letter where there is no duplicate.
Constraints
- The given string can be empty
- The string will only contain lowercase/uppercase alphabets and numerals
- Expected time complexity :
O(n)
- Expected space complexity :
O(A)
whereA
is the number of ASCII characters
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
45
"`firstNonRepeat('asdfsdafdasfjdfsafnnunlfdffvxcvsfansd')` should return `'j'`"
var assert = require('assert');
​
function firstNonRepeat(str) {
// fill this in
return str;
}
​
try {
assert.equal(firstNonRepeat(''), '');
​
console.log('PASSED: ' + "`firstNonRepeat('')` should return `''`");
} catch (err) {
console.log(err);
}
​
try {
assert.equal(firstNonRepeat('a'), 'a');
​
console.log('PASSED: ' + "`firstNonRepeat('a')` should return `'a'`");
} catch (err) {
console.log(err);
}
​
try {
assert.equal(firstNonRepeat('oh my god dude where is my car'), 'g');
​
console.log(
'PASSED: ' +
"`firstNonRepeat('oh my god dude where is my car')` should return `'g'`"
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Here's how we would solve this problem...
How do I use this guide?