Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum. Tags : Interview questions on String, Solution in Java, Duplicate characters, Iterator, HashSet, LinkedHashSet, for loop, while loop, if statement, char[] array, String functions.
About Author:
I am simple guy with lot of ambitions. My main motive is to share whatever knowledge I have related to programming. With me you can easily learn how to solve any programming problem in Java.You can connect with me on social networking sites also.
Can't we solve this problem in little simpler and short way. Because we have to just count the number of characters that are repeated. Can't we do it just using HashMap(). If we then the solution will become very small and easy to understand. May be I am wrong. Please update me on this
We have to print duplicate characters in String. Yes, we can do this problem using HashMap() but I don't think so that is short and simple because we have to perform following steps.
1) Break string into characters. 2) Found the count corresponding to each character -> To achieve this we need nested loop. 3) Take character as a Key and character count as a value, then store it into HashMap(). 4) In HashMap() check for which Key , Value is greater than 1 and finally print Keys.
If you have any better approach to solve this problem using HashMap() please share with us. I will be more happy to see your approach :)
I think we can solve this problem using following steps, HashMap map, Count=0, for i=0, i<length(string), i++ { Chari=string.charat(i), If(map.haskey(chari)) Count++ Else Map.add(chari,0), }
Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.
Hello Rohit,
ReplyDeleteCan't we solve this problem in little simpler and short way.
Because we have to just count the number of characters that are repeated.
Can't we do it just using HashMap().
If we then the solution will become very small and easy to understand.
May be I am wrong. Please update me on this
Hi Piyush,
DeleteWe have to print duplicate characters in String. Yes, we can do this problem using HashMap() but I don't think so that is short and simple because we have to perform following steps.
1) Break string into characters.
2) Found the count corresponding to each character -> To achieve this we need nested loop.
3) Take character as a Key and character count as a value, then store it into HashMap().
4) In HashMap() check for which Key , Value is greater than 1 and finally print Keys.
If you have any better approach to solve this problem using HashMap() please share with us. I will be more happy to see your approach :)
Hello Rohit
DeleteI think we can solve this problem using following steps,
HashMap map,
Count=0,
for i=0, i<length(string), i++
{
Chari=string.charat(i),
If(map.haskey(chari))
Count++
Else
Map.add(chari,0),
}
I don't know the exact function name but I think we can do it in this manner.
DeletePlease response..
Thank you.
Nice article String Programs in Java
ReplyDeletethanks.
Thank you Anurag.
DeleteRohit bhai appke hashset article mere interview clear me kaam aaya. Sir ji aapka bahut thanks.
ReplyDeleteCheers,
http://www.flowerbrackets.com/hashset-java/