How can I find unique elements in a string in the best way?
Sample string format is
myString = "34345667543"
o/p
['3','4','3','5'.....]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is an interesting question, and since it returns so many almost similar results, I did a simple benchmark to decide which is actually the best solution:
and the results of this test are, not entirely surprising (0n 1.8.7p352):
and on 1.9.2p180 :
The results for REE (1.8.7) are close to 1.9.2 :
For fun, I also tried on rubinius:
So while the
split('\\').uniqwins points for readability, thechars.to_a.uniqis almost double as fast.It is weird to notice that on rubinius the
Setsolution is the fastest, but no where near as fast as thechars.to_a.uniqon 1.9.2.