Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing duplucate values in String 1

Status
Not open for further replies.

vasilek

Programmer
Jan 5, 2003
99
US
I have a String:

var s = value1+' '+value2+' '+value1+' '+value3+' '+value2;

How to remove duplicate values from a String?
 
>>var s = value1+' '+value2+' '+value1+' '+value3+' '+value2;
u urself are adding the duplicate values,
var s = value1+' '+value2+' '+value3;

Known is handfull, Unknown is worldfull
 
??? The string gets generated dynamicaly, so I need to remove the duplicate values dynamicaly too.
 
how do u generate the string?

Known is handfull, Unknown is worldfull
 
>>var s = value1+' '+value2+' '+value1+' '+value3+' '+value2;

seems hardcoded. thats why i had my doubts, give me some time to come up with a program(and i think xutopia gave u one)...

Known is handfull, Unknown is worldfull
 
My favourite way is probably the following:
Code:
function removeDuplicates( dupeArray ) {

	var unduped = new Object; var unique = new Array;

	for ( i = 0; i < dupeArray.length; i++ ) {

		unduped[ dupeArray[ i ] ] = dupeArray[ i ];
	}

	for ( k in unduped ) {

		unique.push( unduped[ k ] );
	}

	return unique;
}

var s = &quot;Tom Dick Harry Bob Tom Dick&quot;; // alert( s );
s_unique = removeDuplicates( s.split( &quot; &quot; ) ); // alert( s_unique.join( &quot; &quot; ) );

I've commented out the
Code:
alerts()
but you can put them back in if you want to see what's going on.

HTH.
 
vbkris, thanks for trying to help. yes xutopia gave me the code that removes duplicates. it work OK on Windows platform, but doesn't on Mac in IE5.1, didn't check for Netscape and Safari.
 
A-ha! I found the other thread you guys were talking about. :)

I don't see anything wrong with xutopia's code, but I don't have a Mac to test it on either.

Does the method I gave you above work?
 
Here's a method using Reg Exp:
Code:
<html><head><script>
function remdup(str) {
d = document.tst.tstval;
var ckvals = str.split(&quot; &quot;);
tmp = str;
  for(i=0;i<ckvals.length;i++) {
   retmp = new RegExp(ckvals[i],&quot;g&quot;);
  found = tmp.match(retmp);
   if(found.length > 1) {
	retmp = new RegExp(ckvals[i]); 
	ckvals.splice(i,1);	
	tmp = tmp.replace(retmp,&quot;&quot;); i--;}
 	}
d.value = ckvals.join(&quot; &quot;);}
</script></head><body><form name=&quot;tst&quot;>
<input type=&quot;text&quot; name=&quot;tstval&quot; size=&quot;40&quot; value=&quot;val1 val2 val3 val4 val2 val2&quot;>
<input type=&quot;button&quot; value=&quot;Rem Dup&quot; onClick=&quot;remdup(document.tst.tstval.value);&quot;></form>
</body></html>

2b||!2b
 
Thanks Lrnmore a star for you as I'll be using your code slightly modified.
 
Hello everyone. Thanks for your codes. Finally I got the chance to test them on the Mac OS9. Here are the results:

theboyhope, only the first alert is working, the second is not even poping up.

Lrnmore, clicking on the 'Rem Dup' button doesnot produce any results/changes.

I am just curious how many of you guys or other developers (if you have such information) actually test your code on Macs?

I am not a Mac person myself, just happened to test some of my previous websites on Mac and was surprised that some of the things weren't the way they should've been.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top