Skip to content

Commit

Permalink
Added DuplicateChars Java program and updated the README file (#28)
Browse files Browse the repository at this point in the history
* 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
mepox and axrav authored Oct 8, 2022
1 parent 9b3b137 commit 99d6e1a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
33 changes: 33 additions & 0 deletions Java/DuplicateChars.java
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
}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ Language
├─ linkedlist.
├─ selectionsort.
├─ mergesort.
└─ armstrong_no.
├─ Armstrong_no.
└─ duplicatechars.
```
Expand Down

0 comments on commit 99d6e1a

Please sign in to comment.