• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Duplicate character of a given String

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code is to print duplicate character of a given String.
What does the logic behind these lines of code. Why there are two 'if' statements :                                

if (!duplicates.contains(String.valueOf(str.charAt(j)))) {
                           duplicates += str.charAt(j);
Can someone please explain lines 18 and 19?    
Duplicate.JPG
[Thumbnail for Duplicate.JPG]
Code line 18 and 19
 
Saloon Keeper
Posts: 14501
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider what would happen without the second if-statement for the input strings "aabaa" and "aaa".

Please, in the future don't post screenshots of code. Copy the code instead, and surround it in code tags.
 
Greenhorn
Posts: 26
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your code, the string duplicates starts out as a blank right ? With those 2 FOR loops , you are basically trying to take each char from your input string and compare it to the remaining chars in the string to see if there are duplicates. When you find a match , you are trying to append that char to the string duplicates.

But before appending that char to the string duplicates , you want to make sure you are not adding a char that was already added prior (could happen when your input string is aaaaa bbbbbb) . That is what that IF statement is checking . It is making sure that the string duplicate does not already contain the char str.charAt(j) prior to appending.

The contains method of string will return true if str.charAt(j) is already part of duplicates.

Sibi Varghese wrote:This code is to print duplicate character of a given String.
What does the logic behind these lines of code. Why there are two 'if' statements :                                

if (!duplicates.contains(String.valueOf(str.charAt(j)))) {
                           duplicates += str.charAt(j);
Can someone please explain lines 18 and 19?    

 
Sheriff
Posts: 17345
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The logic of those two if-statements is basically this:
 
Marshal
Posts: 76872
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That suggests to me that a List isn't the correct datatype; a Set<Character> would identify which letters have already been seen much sooner.
 
A berm makes a great wind break. And we all like to break wind once in a while. Like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic