aspose file tools*
The moose likes Beginning Java and the fly likes how to remove duplicate characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to remove duplicate characters" Watch "how to remove duplicate characters" New topic
Author

how to remove duplicate characters

lynn fann
Ranch Hand

Joined: Oct 15, 2006
Posts: 115
for the string "anexample", i want to remove the duplicate character from that string.

in the end i should get:
"anexmpl".

thanks
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Hi,

Try regular expression

bye for now
sat
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
A different approach, as Lynn wrote "no duplicates":


The rest is up to you.

Bu.
lynn fann
Ranch Hand

Joined: Oct 15, 2006
Posts: 115
can have example? i have never try linkhashset before thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

Originally posted by lynn fann:
can have example? i have never try linkhashset before thanks.


Lynn,

If this is a homework question, I highly doubt your instructor would allow the use of regular expressions, the linkedhashset, or anything else that you have not learned yet.

So... what have you learned? I am assuming that you just learned about loops? Or have you learned about collections yet (which will allow the set)?

In any case, I would recommend that you start with what you know, and start to build the example yourself. You will get more responses, if you ask a question about an issue, once you tried it first.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
lynn fann
Ranch Hand

Joined: Oct 15, 2006
Posts: 115
is NOT a homework. is an urgent project.

i need to remove the duplicate characters, how should i do it. if you not going to help, pls dont reply

thanks.
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182
it looks like homework PROJECT to me
lynn fann
Ranch Hand

Joined: Oct 15, 2006
Posts: 115
IF YOU ARE NOT GOING TO HELP, THEN DONT TALK SO MUCH. IS A COMPANY PROJECT ALREADY. IM NEW TO JAVA.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

Originally posted by lynn fann:
IF YOU ARE NOT GOING TO HELP, THEN DONT TALK SO MUCH. IS A COMPANY PROJECT ALREADY. IM NEW TO JAVA.


Lynn,

JavaRanch is a site devoted to Learning Java. It is also a moderated site. As such, you are requested in following certain rules -- including this, this, and this. Now, as for this last one, this may not be homework, but it is a common question that is used for learning java.

Now you are getting close to violating the Ranch's "Be Nice" rule, which is not tolerated here.

Henry
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
(I can't see it as being anything but homework)

Anyway, you would do it exactly the same way as if you were doing it on paper:
look at the letter, then look at the other letters and remove matches.

the String methods you are likely to use are
charAt()
indexOf()
lastIndexOf()

and, if you end up doing this in a loop, it might be better for you to
work backwards
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I actually kind of liked the Set approach. Look at the doc - What does it do if you try to put in two objects that are equals()? You could put every letter in the string and the Set would contain exactly what you're after. It's likely less efficient - have to convert chars into objects of some kind - but pretty well self-documenting.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
 
subject: how to remove duplicate characters