-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DuplicateChars Java program and updated the README file (#28)
* add: DuplicateChars Java program and updated the README file * refactor: the code to look nicer and to use static inputs Co-authored-by: Aarav Arora <[email protected]>
- Loading branch information
Showing
2 changed files
with
35 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Check if the given String has duplicate characters, ignoring case. | ||
public class DuplicateChars { | ||
|
||
public static void main(String[] args) { | ||
String str1 = "duplicate"; | ||
String str2 = "character"; | ||
|
||
System.out.println("'" + str1 + "' contains duplicate characters? " + checkDuplicateChars(str1)); | ||
System.out.println("'" + str2 + "' contains duplicate characters? " + checkDuplicateChars(str2)); | ||
} | ||
|
||
static boolean checkDuplicateChars(String str) { | ||
// Trim leading and trailing spaces | ||
str = str.trim(); | ||
|
||
// Convert all the characters in the String to lower case | ||
str = str.toLowerCase(); | ||
|
||
// Convert the given string to a char array | ||
char[] chars = str.toCharArray(); | ||
|
||
// Check each characters present in the string | ||
for (int i = 0; i < chars.length; i++) { | ||
for (int j = i + 1; j < chars.length; j++) { | ||
if (chars[i] == chars[j]) { | ||
return true; // Found a duplicate character | ||
} | ||
} | ||
} | ||
|
||
return false; // No duplicate character found | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters