File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes finding duplicate characters in a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "finding duplicate characters in a String" Watch "finding duplicate characters in a String" New topic
Author

finding duplicate characters in a String

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi ,

I have this String "Mumbai"

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


Save India From Corruption - Anna Hazare.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Pnecil, paper and eraser. Write down how you intend to do it, then later convert it to code.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Ravi Kiran Va wrote: . . . I have this String "Mumbai" . . . find the duplicate character ??
What duplicate character?
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

please tell me how to find duplicate characters in a String??(It can be any character)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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
Sheriff

Joined: Sep 28, 2004
Posts: 18116
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

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

Joined: Oct 13, 2005
Posts: 36486
    
  16
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

Joined: Sep 07, 2012
Posts: 71

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

Joined: Aug 16, 2005
Posts: 13875
    
  10

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++;

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 71

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

Joined: Sep 07, 2012
Posts: 71

here is the general code to find how many times the single duplicated appear in the string

Thanks
Gubendran Lakshmanan
Greenhorn

Joined: Dec 19, 2013
Posts: 2
You can use google Guava collections "Multiset". Below is the code.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Welcome to the Ranch
li stayhere
Greenhorn

Joined: Jan 03, 2014
Posts: 1
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

Joined: Oct 13, 2005
Posts: 36486
    
  16
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

Joined: Mar 17, 2011
Posts: 7052
    
  16

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


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finding duplicate characters in a String
 
Similar Threads
Very scary thing
how to remove duplicate characters
Date & Time Problem
MS/Accessand unicode
to find out the size and time of jar that is downloaded using webstart