• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Duplicate Characters

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think i am doing wrong something in print statement .....i tried the logic its showing the output right(on paper)
 
Sheriff
Posts: 28371
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Failing?"
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
William, please don't post full answers. LetThemDoTheirOwnHomework, and DontBeACodeMill.
 
deepak carter
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will write that code again today and will come back with the outcome
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would consider using a map here to keep a count of each character found.
 
Marshal
Posts: 80288
433
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll definitely have to (belatedly) agree with the consensus here, a Map<Char, Integer> is the simplest way to go.

 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean Map<Character,Integer>. There is no Char class.
 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoops. . . yes, that's what I meant. Map<Character, Integer>

Ugh, that's slightly embarrassing...

 
I am a man of mystery. Mostly because of this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic