Java program to remove duplicate characters from a string

In this post, you will learn how to find duplicate characters in a string in Java and also method to find the duplicate characters in a string.

Problem Statement:

You are given a string of characters, and you need to find the duplicate characters in that string using Java program.

Duplicate Characters in a string are those characters that occur more than once in the string.

For Example:

Input :
string is : programming
Output :
Duplicate characters in this string :  
r
g
m

Explanation:

in above input string – programming, p occurs once, r occurs twice, o occurs once, g occurs twice, m occurs twice and i, n is occurred once. So, r, g and m are the duplicate characters in this string.

Solution :

The methods are as follows:

Algorithm:

First, we will take a string as input.

then use two loops to find out the duplicate characters.

The outer loop will be used to select each character of the string.

The inner loop will be used to count the frequency of the selected character and set the character as visited.

If the frequency of the selected character is greater than one, then print it.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class DuplicateCharacters {
public static void main(String[] args) {
// input string
String string = "programming";
System. out.println("The string is: " + string);
// covert the string to the char array
char s[] = string.toCharArray();
int i = 0;
// Traverse the string from left to right
System.out.print("The duplicate characters in this string are: ");
for (i = 0; i < s.length; i++) {
// For each character count the frequency
int count = 1;
// s[i] == '0' means we have already visited this character so no need to count its frequency again.
if (s[i] == '0')
continue;
int j = i + 1;
for (j = i + 1; j < s.length; j++) {
// If a match found increase the count by 1
if (s[i] == s[j]) {
count++;
s[j] = '0';
}
}
// If count is more than one then print it
if (count > 1) {
System.out.print(s[i] + " ");
}
}
}
public class DuplicateCharacters { public static void main(String[] args) { // input string String string = "programming"; System. out.println("The string is: " + string); // covert the string to the char array char s[] = string.toCharArray(); int i = 0; // Traverse the string from left to right System.out.print("The duplicate characters in this string are: "); for (i = 0; i < s.length; i++) { // For each character count the frequency int count = 1; // s[i] == '0' means we have already visited this character so no need to count its frequency again. if (s[i] == '0') continue; int j = i + 1; for (j = i + 1; j < s.length; j++) { // If a match found increase the count by 1 if (s[i] == s[j]) { count++; s[j] = '0'; } } // If count is more than one then print it if (count > 1) { System.out.print(s[i] + " "); } } }
public class DuplicateCharacters {
    public static void main(String[] args) {
        // input string
        String string = "programming";
        System. out.println("The string is: " + string);
 
        // covert the string to the char array
        char s[] = string.toCharArray();
        int i = 0;
        // Traverse the string from left to right
        System.out.print("The duplicate characters in this string are: ");
        for (i = 0; i < s.length; i++) {
            // For each character count the frequency
            int count = 1;
            // s[i] == '0' means we have already visited this character so no need to count its frequency again.
            if (s[i] == '0')
                continue;
            int j = i + 1;
            for (j = i + 1; j < s.length; j++) {
                // If a match found increase the count by 1
                if (s[i] == s[j]) {
                    count++;
                    s[j] = '0';
                }
            }
 
            // If count is more than one then print it
            if (count > 1) {
                System.out.print(s[i] + " ");
            }
        }
    }

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
The string is: programming
The duplicate characters in this string are: r g m
The string is: programming The duplicate characters in this string are: r g m
The string is: programming
The duplicate characters in this string are: r g m

More string programs in java :

Java program to Count the Total number of Characters in a String

Java program to reverse a string

Java Program to check if a String is Palindrome or not

 

 

Leave a Comment

%d bloggers like this:
Python4U
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.