Identifying duplicate characters in a string is a common programming task with numerous practical applications. Whether you’re working on text analysis, data cleansing, or any project involving string manipulation, knowing how to find duplicate characters is a valuable skill. In this blog, we will explore how to achieve this in Java, covering both pre-defined and user-defined strings.
Our primary objectives are twofold: first, to demonstrate how to detect duplicate characters in pre-defined strings efficiently, and second, to show how to accept and process user-defined strings to find duplicates. By the end of this tutorial, you’ll be well-equipped to tackle duplicate character detection tasks in your Java projects effectively.
Learn How to Generate Random Numbers in Python, Now!
Program Java String is pre-defined
// Java Program to Find the Duplicate Characters in a String public class DuplicateCharactersEx { public static void main(String[] args) { String string1 = "Newtum Solutions"; int count; //Converts given string into character array char string[] = string1.toCharArray(); System.out.println("Duplicate characters in a given string: "); //Counts each character present in the string for(int i = 0; i <string.length; i++) { count = 1; for(int j = i+1; j <string.length; j++) { if(string[i] == string[j] && string[i] != ' ') { count++; //Set string[j] to 0 to avoid printing visited character string[j] = '0'; } } //A character is considered as duplicate if count is greater than 1 if(count > 1 && string[i] != '0') System.out.println(string[i]); } } }
Output:
Duplicate characters in a given string:
t
u
o
User defined string
// Java Program to Find the Duplicate Characters in a String import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the string is: "); String string=sc.nextLine(); int count; System.out.println("The entered string is: "+string); //Converts given string into character array char str[] = string.toCharArray(); System.out.println("Duplicate characters in a given string: "); //Count the frequency of each character present in the string for(int i = 0; i <str.length; i++) { count = 1; for(int j = i+1; j <str.length; j++) { if(str[i] == str[j] && str[i] != ' ') { count++; //Set string[j] to 0 to avoid printing visited character str[j] = '0'; } } //A character is considered as duplicate if count is greater than 1 if(count > 1 && str[i] != '0') System.out.println(str[i]); } } }
Check out our blog on Duplicate Elements in Array in Java here!
Output:
Enter the string is:
welcome
The entered string is: welcome
Duplicate characters in a given string:
e
Combining Pre-defined and User-defined String Detection
In some scenarios, you may need to find duplicate characters in both pre-defined and user-defined strings within the same program. The good news is that the techniques demonstrated for each type of string detection can be combined seamlessly in your Java projects. By creating reusable functions or methods, you can efficiently handle both scenarios.
1. Create Reusable Functions or Methods:
– Define functions or methods that encapsulate the logic for detecting duplicate characters. These functions should accept a string as a parameter and return the result.
– By creating reusable functions, you can apply the same duplicate character detection logic to both pre-defined and user-defined strings without duplicating code.
2. Accept Input Dynamically:
– Design your program to accept input dynamically. You can use Java’s `Scanner` class to read user input from the console or create a user-friendly interface for entering strings.
– For pre-defined strings, you can call the functions with the string values directly. For user-defined strings, call the functions with the input obtained from the user.
Get complete Java Programming Exercises and Solutions here!
3. Combine and Manage Results:
– When you have results from both pre-defined and user-defined strings, you can combine and manage them as needed.
– You might store the results in data structures like arrays, lists, or maps to facilitate further processing or display.
4. Modularity and Code Reuse:
– Emphasize modularity and code reuse in your program’s design. Keep the logic for detecting duplicate characters separate from input handling and result management.
– This modular approach makes your code more maintainable and allows you to extend it easily in the future.
5. User Guidance and Error Handling:
– Implement user guidance and error handling for user-defined string input. Ensure that the program handles incorrect or unexpected input gracefully, providing feedback to the user.