From 99d6e1adc1a4b96b132c1f3a1a957efb9b517470 Mon Sep 17 00:00:00 2001 From: mepox <21198248+mepox@users.noreply.github.com> Date: Sat, 8 Oct 2022 13:52:36 +0100 Subject: [PATCH] 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 --- Java/DuplicateChars.java | 33 +++++++++++++++++++++++++++++++++ README.md | 3 ++- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 Java/DuplicateChars.java diff --git a/Java/DuplicateChars.java b/Java/DuplicateChars.java new file mode 100644 index 0000000..a20b9d5 --- /dev/null +++ b/Java/DuplicateChars.java @@ -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 + } +} \ No newline at end of file diff --git a/README.md b/README.md index bf5e805..135e5a9 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,8 @@ Language ├─ linkedlist. ├─ selectionsort. ├─ mergesort. -└─ armstrong_no. +├─ Armstrong_no. +└─ duplicatechars. ```