For example. bacdbecdfb
First non-repetitive character would be ‘a’ , second would be ‘e’ .
This was an interview question, I came up with using hashing to solve in 2 passes but the interviewer wanted it in one pass.
The hint he gave was that the maximum value of k can be 26.
You have just 26 possible character, So you can create an array of int of size 26 and in this way you can find all non repeated characters (by traversing your input and increase related character number). another iteration is needed to find their order (also repeated or not in second iterate is possible, in first iteration you just set the numbers for characters, e.g
ais in position 0 and seen 1 time).This known as counting sort.
Edit: for doing this just in one path you need add extra information to your sorting data, in fact you should add first visited time, So you will have array of struct:
be careful
sorterhas length 26, and also justOnes has length ≤ 26 so iterating them 100 times doesn’t related to input data iteration and has no problem, Also sorting or other actions all are constants.