By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,630 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,630 IT Pros & Developers. It's quick & easy.

Removing Duplicates from a string

P: n/a
Hi all -

I was wondering if Javascript has a way to easily remove duplicates
from a string. For example, if I had a string:

"car truck car truck truck tree post post tree"

it should turn into:

"car truck tree post"

or something to that effect. The order of words does not matter for
me, so it does not have to be sorted in any particular way. If anyone
can shed some light on this I would greatly appreciate it. Thanks in
advance.

Oct 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

M B HONG 20 wrote:
Hi all -

I was wondering if Javascript has a way to easily remove duplicates
from a string. For example, if I had a string:

"car truck car truck truck tree post post tree"

it should turn into:

"car truck tree post"

or something to that effect. The order of words does not matter for
me, so it does not have to be sorted in any particular way. If anyone
can shed some light on this I would greatly appreciate it. Thanks in
advance.


Hi M.B.

There is no built in method to remove duplicates from a string. But
you can make one. This is going off the top of my head so the code is
not complete:

function rmDuplicates(myStr)
{
/*take your string, split, sort, then join them back together.
your duplicates will be sorted and will be in order.
this should make it easier to remove if they are in a consecutive
pattern*/

var tmp = myStr;
var myArray = tmp.split(" ");
myArray.sort();
tmp = myArray.join(" ");

//use regular expression to find consecutive patterns and replace
...code...
}

Hope this will get you started on the right path.

Oct 18 '05 #2

P: n/a
M B HONG 20 wrote:
I was wondering if Javascript has a way to easily remove duplicates
from a string. For example, if I had a string:

"car truck car truck truck tree post post tree"

it should turn into:

"car truck tree post"
[...]


JS objects based on Object are best for recognizing and removing dupes.
Quick hack (`s' refers to the string containing dupes):

for (var a = s.split(/\s+/), i = a.length, o = {}; i--;)
{
if (i < a.length - 1)
s += ' ';

if (!o[a[i]]) // [1]
s += a[i];
else
o[a[i]] = true;
}

or use

if (typeof o[a[i]] == "undefined")

instead of [1]. After that, `s' then refers to the reduced string.
One could also use another array, push elements and join them afterwards
instead of string concatenation, but after all it's only a quick hack.
HTH

PointedEars
Oct 18 '05 #3

P: n/a
(Forget about my previous nonsense on this subject, I cancelled it.)

M B HONG 20 wrote:
I was wondering if Javascript has a way to easily remove duplicates
from a string. For example, if I had a string:

"car truck car truck truck tree post post tree"

it should turn into:

"car truck tree post"
[...]


JS objects based on Object are best for recognizing and removing dupes.
Quick hack (`s' refers to the string containing dupes):

for (var a = s.split(/\s+/), i = 0, o = {}, s2 = ''; i < a.length; i++)
{
if (i > 0)
s2 += ' ';

if (!o[a[i]])
{
s2 += a[i];
o[a[i]] = true;
}
}

or use

if (typeof o[a[i]] == "undefined")

instead of [1]. After that, `s2' refers to the reduced string.
One could also use another array, push elements and join them afterwards
instead of string concatenation, but after all it's only a quick hack.
HTH

PointedEars
Oct 18 '05 #4

P: n/a
Thanks a lot for your input, guys.

Oct 18 '05 #5

P: n/a
Thomas 'PointedEars' Lahn said the following on 10/18/2005 3:32 PM:
(Forget about my previous nonsense on this subject, I cancelled it.)


No, you didn't cancel it, you just thought you did.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 18 '05 #6

P: n/a
See if this or a similar regex solution will work.

inText = "car truck car truck truck tree post post tree";
rx = /\b(\w+)(?:(\s+)\1\b)+/g
outText = inText.replace(rx,'$1$2');
If the non-capture group (?: is not supported, change the regex and
ordinals to:
rx = /\b(\w+)((\s+)\1\b)+/g
outText = inText.replace(rx,'$1$3');
Also, if you need other non-word-character stuff in between like a tag
then replace (\s+) with ((?:\s|<[^>]+>)+)

Oct 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.