Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6323091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:27:13+00:00 2026-05-24T16:27:13+00:00

How can I find unique elements in a string in the best way? Sample

  • 0

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'.....]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:27:13+00:00Added an answer on May 24, 2026 at 4:27 pm

    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:

    require 'rubygems'
    require 'benchmark'
    require 'set'
    
    puts "Do the test"
    
    Benchmark.bm(40) do |x|
    
      STRING_TEST = "26263636362626218118181111232112233"
    
      x.report("do split and uniq") do
        (1..1000000).each { STRING_TEST.split(//).uniq }
      end
    
      x.report("do chars to_a uniq") do
        (1..1000000).each { STRING_TEST.chars.to_a.uniq }
      end
    
      x.report("using Set") do
        (1..1000000).each { Set.new(STRING_TEST.split('')).to_a }
      end
    
    end
    

    and the results of this test are, not entirely surprising (0n 1.8.7p352):

                                                  user     system      total        real
    do split and uniq                        27.060000   0.000000  27.060000 ( 27.084629)
    do chars to_a uniq                       14.440000   0.000000  14.440000 ( 14.452377)
    using Set                                41.740000   0.000000  41.740000 ( 41.760313)
    

    and on 1.9.2p180 :

                                                  user     system      total        real
    do split and uniq                        19.260000   0.000000  19.260000 ( 19.242727)
    do chars to_a uniq                        8.980000   0.010000   8.990000 (  8.983891)
    using Set                                28.220000   0.000000  28.220000 ( 28.186787)
    

    The results for REE (1.8.7) are close to 1.9.2 :

                                                  user     system      total        real
    do split and uniq                        19.120000   0.000000  19.120000 ( 19.126034)
    do chars to_a uniq                       14.740000   0.010000  14.750000 ( 14.766540)
    using Set                                32.770000   0.120000  32.890000 ( 32.921878)
    

    For fun, I also tried on rubinius:

                                                  user     system      total        real
    do split and uniq                        26.100000   0.000000  26.100000 ( 26.651468)
    do chars to_a uniq                       25.680000   0.000000  25.680000 ( 25.780944)
    using Set                                22.500000   0.000000  22.500000 ( 22.649291)
    

    So while the split('\\').uniq wins points for readability, the chars.to_a.uniq is almost double as fast.

    It is weird to notice that on rubinius the Set solution is the fastest, but no where near as fast as the chars.to_a.uniq on 1.9.2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://api.jquery.com/jQuery.unique/ lets you get only unique elements. Is there I can find out if
Where can find resources about best practices for SharePoint programming? I am talking about
I can find no non-deprecated way of hiding an item in a menu bar
I have got an array containing unique elements. I need to find out the
I can find tutorials about mapping textures to polygons specifying vertices etc. but nothing
I can find lots of information on how Long Polling works (For example, this
I can find a direct table foreign key in c# like: foreach (ForeignKey key
How can find max of three numbers in XSL ? More Information : I
I can find the definition files at http://www.php.net/~helly/php/ext/spl/... but I want to extend DirectoryIterator
Can somebody tell me how I can find out how many threads are in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.