Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finding duplicate characters in a String

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I have this String "Mumbai"

Please tell me how can i parse through the contents of this String and find the
duplicate character ??

 
Campbell Ritchie
Sheriff
Pie
Posts: 49393
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pnecil, paper and eraser. Write down how you intend to do it, then later convert it to code.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49393
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Kiran Va wrote: . . . I have this String "Mumbai" . . . find the duplicate character ??
What duplicate character?
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please tell me how to find duplicate characters in a String??(It can be any character)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12145
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, you have to define the problem better. Does 'M' equal 'm'? Some people would say yes, others would say no. Until you define the conditions, there is no point in answering.

Next...please read Campbell's first post. Before you write any code, you should have a good idea how you'd do it using nothing more than paper, pencil, and your brain. Don't even consider writing a single line of code until you can write down in a natural language (English, German, Hindi, Esperanto...) how to do it.

Once you have that, writing the java code (or C, or Perl, or assembly) is almost trivial, assuming your algorithm above is well defined.

So, work on that, and when you have something, post it here, and we'll work on the next steps.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am searching the net for this , didn't find anything related so the reason i posted

Exmple in the below case , there are two a's repeated .
But i dont want to use it staticlly as str.charAt(i)=='a' .

I want to know dynamically which character is repeating and how many number of times




 
Henry Wong
author
Marshal
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll add to the chorus to agree with Campbell. Do it on paper first. After all, if you can't do it on paper, how do you expect to teach the computer how to do it?

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12145
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case it isn't obvious already, nobody here is going to simply hand you the answer. we're here to help you, not do it for you.

Again, forget about writing a single line of java code. If you didn't have access to a computer, how would YOU find duplicate characters?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49393
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote: . . . Campbell's first post. . . . paper, pencil, and your brain. . . .
Good grief! Somebody has used "Campbell" and "brain" on the same line.

Joking aside, Ravi Kiran Va, you are mistaken even to write a class name until you know what you plan to do with it. You didn't say you wanted to know how many repeats there are. Go through the Java™ Tutorials section on Collections, and there is an example which does something surprisingly similar to what you want in there somewhere.
 
upanshu vaid
Ranch Hand
Posts: 83
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I accidently hit with this thread and as i have a similar program so I am posting the below code it will help you

Thanks
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15359
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, upanshu vaid.

There are a few minor issues with your code.

First of all, it doesn't really work. The only thing that your code does is find which characters in the string are 'l' or 'L'. Suppose you didn't know beforehand which characters were duplicates - this code would not find them. For example, if I change line 3 to this:

String g = "Mumbai";

then your code would not find the two m's.

Second, you should use the logical OR operator || instead of the bitwise OR operator | in line 6.

Third, line 8 works, but this is not how you should increment the counter: count=++count;

Just write this instead: count++;
 
upanshu vaid
Ranch Hand
Posts: 83
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper de Jong
for your value able points , the code that I wrote was just to show the duplicate character can be find in string , I use static string , now I think I should write that a program that will take string from user and then find the duplicates.
Thanks
 
upanshu vaid
Ranch Hand
Posts: 83
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the general code to find how many times the single duplicated appear in the string

Thanks
 
Gubendran Lakshmanan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use google Guava collections "Multiset". Below is the code.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49393
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
li stayhere
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch


That's too complex. Here is an example in Java, I've tested it. I hope it can be help to you.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49393
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I can see some complex features in your code, too. Changing to lower case and then subtracting to give numbers between 0 and 25 looks complex to me. Also using continue when a simple if is all you need.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
li stayhere wrote:That's too complex...

And you're re-inventing the wheel. Why not use classes that are already written to help you out?:I should add that it's only one way of doing it; there are many others.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic