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 Duplicate Characters 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 "Duplicate Characters" Watch "Duplicate Characters" New topic
Author

Duplicate Characters

deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
Hi All,

I am writing a program to know duplicate characters present in a String

This is what i have written.




its working fine there is only single duplicate character present but failing when more than one duplicate charater is present.

Any help.???

deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
i think i am doing wrong something in print statement .....i tried the logic its showing the output right(on paper)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

"Failing?"
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

ts working fine there is only single duplicate character present but failing when more than one duplicate charater is present.

Can you tell more precisely what do you meant by failing and what it mean to have single duplicate character.

Also assume one case when the String is like "The god is with us in our life" what will happen when second 'i' is assigned to char ch1 at line 9 ?.
will it produce correct results ?


Do not wait to strike till the iron is hot; but make it hot by striking....
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

also you are printing your values at wrong/inappropriate place.
put the output statement after the loop completed and once the complete string is processed...
deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
failing doesnt mean that its showing error. i mean to say that consider a string "deepppak" its showing output like this

e 2
p 2
p 3
p 2
however if the string is "deeppak" its showing correct output which is

e 2
p 2

hope i have made myself clear
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

As I mentioned earlier you have placed your print statement at wrong place.
Even If you put your print statement at right place, your program will not work correctly if single character appears thrice or more.
You can handle this situation by removing all occurrence of the character once it is processed first time, so that it doesn't get counted again.
Joe Vahabzadeh
Ranch Hand

Joined: Jan 05, 2005
Posts: 140
To expand on Manoj's question...

What is the output you WANT to have in the situation like:
"Deeppeeek"

Should it be
e:2
p:2
e:3

or are you expecting
e:5
p:2

That will affect what I would suggest in terms of code changes.
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

Sometimes a different approach can be more effective than indexing and substringing.

I remember answering this question at an interview using the method I will now show, and blowing them away!



With "Deeppak":

With "Deepppak":

With "The god is with us in our life"

Tweak it all you want if you want sorting or any of that stuff.

Pat.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

William, please don't post full answers. LetThemDoTheirOwnHomework, and DontBeACodeMill.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
I will write that code again today and will come back with the outcome
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

I would consider using a map here to keep a count of each character found.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
In which case you should not say which part of the Java™ tutorials has an example in, so similar to what the current poster wants!
deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
I dont want to use map as i dont want to complicate thing.(might become easier with using Map but don't want that).


and regarding Joe Vahabzadeh query ....i am looking for output like this


e:5
p:2

if the input is

Deeppeeek
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
deepak carter wrote:I dont want to use map as i dont want to complicate thing.(might become easier with using Map but don't want that).


There really is no simpler solution than using a Map. However, an alternative is to design your own class that has a character attribute and a count attribute. Then when you are analyzing a string you can instantiate objects of this class as needed, store them in a set, and increment their counts as needed. (This approach is rather more effortful than using a Map).

Edit: if you don't mind the wasted space, you could use an int array whose indices correspond to the int value of characters. The value at each index would be that char's count in the string. This approach may not be appropriate in a production setting, but it is simple, quick and easy.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Dennis Deems wrote:There really is no simpler solution than using a Map.

There is the possibility of using an int[] where you use chars for the indexes. This does require an int[] of length 65536 though, unless the text is limited to only ASCII, in which case the array only needs 128 elements.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Rob Spoor wrote:
Dennis Deems wrote:There really is no simpler solution than using a Map.

There is the possibility of using an int[] where you use chars for the indexes. This does require an int[] of length 65536 though, unless the text is limited to only ASCII, in which case the array only needs 128 elements.

I raise this possibility in my second paragraph. ;)
Joe Vahabzadeh
Ranch Hand

Joined: Jan 05, 2005
Posts: 140
I'll definitely have to (belatedly) agree with the consensus here, a Map<Char, Integer> is the simplest way to go.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

You mean Map<Character,Integer>. There is no Char class.
Joe Vahabzadeh
Ranch Hand

Joined: Jan 05, 2005
Posts: 140
Whoops. . . yes, that's what I meant. Map<Character, Integer>

Ugh, that's slightly embarrassing...

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Duplicate Characters
 
Similar Threads
Help me out to devise an algorithm for this
characters can not be displaied for codes between 0 and 65,535
returning an array of characters
Ordering of AlphaNumeric No. in ascending Order
convert long to string