473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Removing Duplicates from a string

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
6 3433

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
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
(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
Thanks a lot for your input, guys.

Oct 18 '05 #5
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Rubinho | last post by:
I've a list with duplicate members and I need to make each entry unique. I've come up with two ways of doing it and I'd like some input on what would be considered more pythonic (or at least...
4
by: Drew | last post by:
I have a permission tracking app that I am working on, and I have made the insert page for it. I am having issues on how to prevent duplicates from getting entered. Currently the interface for...
6
by: Marlene | last post by:
Hi All I have the following scenario, where I have found all the duplicates in a table, based on an order number and a part number (item).I might have something like this: Order PODate Rec...
9
by: vbportal | last post by:
Hi, I would like to add BitArrays to an ArrayList and then remove any duplicates - can someone please help me forward. I seem to have (at leaset ;-) )2 problems/lack of understanding (see test...
16
by: tyrfboard | last post by:
I've been searching for awhile now on how to remove duplicates from a table within an Access db and have found plenty of articles on finding or deleting duplicates. All I want to do is remove them...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
7
by: vsgdp | last post by:
I have a container of pointers. It is possible for two pointers to point to the same element. I want to remove duplicates. I am open to which container is best for this. I thought of using...
5
by: maphew | last post by:
Hello, I have some lists for which I need to remove duplicates. I found the sets.Sets() module which does exactly this, but how do I get the set back out again? # existing input: A,B,B,C,D #...
4
by: Mokita | last post by:
Hello, I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script, where I want to eliminate the duplicates...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.