« Alternativa 3D - two new examples
» Converting a flex 2 app to air

ActionScript, How to, Testing

Comparison: Remove Duplicate Chars from a String Via Array and RegExp

Andrei Ionescu | 23.04.08 | 1 Comment

Table of contents

  1. Remove Duplicate Chars from a String
  2. Comparison: Remove Duplicate Chars from a String Via Array and RegExp

In the previous article, Remove Duplicate Chars from a String, I posted a way to remove duplicates characters from a string and make it of unique chars. So to make myself clear I’ll give the following example:

Source string: “aabbccaaddee
Should be: “abcde
(note the underlined characters).

In my previous article TLP has commented and created a better method. You can see his comment here.

His function is very straight forward, is based on the same concept, but is at least twice as faster. The steps are:

  1. split the string to an array
  2. sort the array
  3. join back to string
  4. replace duplicates by only one character

The code is:

private function removeDuplicatesViaRegExp(string:String):String 
{
    return string.split('').sort().join('').replace(/(.)\1+/gi,'$1');
}

So I took both methods and built a new Flex application to test and compare them (see bellow).

I’m sure you’ll want the sources so at the end the source file can be downloaded.

For this new way, using regular expressions, to achieve the transformation to a string with unique characters, the credit goes to TLP. Thanks man. Great job.

Share and Enjoy:
  • Technorati
  • StumbleUpon
  • del.icio.us
  • NewsVine
  • Reddit
  • Digg
  • Furl
  • co.mments
  • blogmarks
  • Slashdot
  • description
  • Taggly
  • YahooMyWeb
  • connotea
  • Webride
removeDuplicates_sources
removeDuplicates




Tags: , , ,

This post was written by Andrei Ionescu

Views: 2999

related

1 Comment

have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">

:

:


« Alternativa 3D - two new examples
» Converting a flex 2 app to air