Table of contents
- Remove Duplicate Chars from a String
- 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:
- split the string to an array
- sort the array
- join back to string
- 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.
| ||
|
Tags: chars, duplicate, string, unique
This post was written by Andrei Ionescu
Views: 2999



















Thanks for the mention Andrei.
And another thanks for posting the source of the application. I do believe that’s the first time I’ve looked over a MXML file and it does a lot to help me understand Flex.