This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin Cookbook and have Ken Kousen on-line!
See this thread for details.
Win a copy of Kotlin Cookbook this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Duplicate character of a given String

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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: 11020
243
  • Mark post as helpful
  • send pies
  • 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: 23
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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: 14622
243
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The logic of those two if-statements is basically this:
 
Marshal
Posts: 67050
255
  • Mark post as helpful
  • send pies
  • 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.
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!