• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to remove duplicate characters

 
lynn fann
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Try regular expression

bye for now
sat
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A different approach, as Lynn wrote "no duplicates":


The rest is up to you.

Bu.
 
lynn fann
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can have example? i have never try linkhashset before thanks.
 
Henry Wong
author
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
lynn fann
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it looks like homework PROJECT to me
 
lynn fann
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IF YOU ARE NOT GOING TO HELP, THEN DONT TALK SO MUCH. IS A COMPANY PROJECT ALREADY. IM NEW TO JAVA.
 
Henry Wong
author
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic