Fx{r} is trying to start the Fx{r} Community! Please join our group on Adobe Groups following this link: http://groups.adobe.com/groups/ab29539ab9.
Fx{r} is now on Twitter too. Follow us @ twitter.com/fx_r!
«
»

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
Google Buzz

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:
  • Twitter
  • Google Buzz
  • LinkedIn
  • Google Bookmarks
  • del.icio.us
  • Digg
  • Sphinn
  • blogmarks
  • Reddit
  • StumbleUpon
  • Facebook
  • DZone
  • FriendFeed
  • Yahoo! Buzz
  • Yahoo! Bookmarks
  • Slashdot
  • MySpace
  • Add to favorites
removeDuplicates_sources
removeDuplicates




Tags: , , ,

This post was written by Andrei Ionescu

Views: 7296

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="" escaped="">

:

:


«
»