Re: How to remove duplicate words from text

Giganews Newsgroups
Subject: Re: How to remove duplicate words from text
Posted by:  Richard Cornford (Richa…
Date: Tue, 22 Jul 2003

"Voetleuce en fênsievry" <carlit…> wrote in message
> I'm not a JavaScript author myself, but I'm looking for a method to
> remove duplicate words from a piece of text.  This text would
> presumably be pasted into a text box.
> I have, for example, a list of town names, but there are hundreds of
> duplicates, like:
> "Aberdeen Aberdeen Aberdeen Edinburg Edinburg Inverness etc."
> Can you direct me to a script (or write one, please) that will remove
> the excess names and leave just one instance of each name, like:
> "Aberdeen Edinburg Inverness etc."

You need to be clearer about the nature of your list. How does a
space-separated list of city/town names cope with 'Chipping Sudbury' or
'Milton Kenes'. Having decided how the list is going to distinguish one
distinct place name from the next (comma separated?) you could use the
String.split method to produce an array of strings - var placeNameArray
= listText.split( [ delimiter string or regular expression] ); - and
then create a new Object and assign a new (maybe boolean true) property
to the Object using each place name from the array as the property name.
Duplicates would only server to re-set an existing property. Then a -
for(var propName in Obj) loop would enumerate the created properties of
the object and could be used to create a duplicate-free list.




In response to

How to remove duplicate words from text posted by carlit… (Voetleuce en fênsievry) on 22 Jul 2003