Find All Duplicate Characters in a String in Java



The duplicate characters in a string are those that occur more than once. In Java, String is a non-primitive datatype that contains one or more characters and is enclosed in double quotes(" ").

As per the problem statement, we are given a string and our task is to write a Java program to find all the duplicate characters from that string. We can use for loop and nested for loop to solve this given problem.


Example Scenario

Input: String str = "Apple";
Output: duplicate_char = p; 

p is a duplicate character as it occurs more than once.

Using for Loop

In this approach, we follow the steps given below ?

  • First convert the given string into character array and then sort it using Arrays.sort() method.
  • Next, use the for loop to iterate over the element of character array.
  • Use if block to check whether current character is equal to the next character.
  • If found equal, mark the character as duplicate and repeat the process till the length of array.

Example

In this Java program, we are using for loop to find and print duplicate characters in a string.

import java.util.Arrays;
public class Example {
   public static void checkDupliChar(String str) {
      System.out.println("The string is: " + str);
      char[] carray = str.toCharArray();
      // sorting the character array
      Arrays.sort(carray);
      System.out.println("Duplicate Characters in the above string are: ");
      // for loop to print duplicate character
      for (int i = 0; i < carray.length - 1; i++) {
         if (carray[i] == carray[i + 1]) {
            System.out.println(carray[i]);
            // loop to skip next occurrence of same character
            while (i < carray.length - 1 && carray[i] == carray[i + 1]) {
               i++;
            }
         }
      }
   }
   // main method
   public static void main(String[] args) {
      checkDupliChar("tutorialspoint");
   }
}

The output of the above code is as follows ?

The string is: tutorialspoint
Duplicate Characters in the above string are: 
i
o
t

Using Nested for Loop

Follow the below steps to find all duplicate character in a string using nested for loop approach ?

  • Convert the given string into character array.
  • Run the outer for loop from index 0 to length of character array.
  • The inner for loop will run from current+1 index to length of the character array.
  • Next, we use if block to check whether current character is equal to the next character.
  • If found equal, print the duplicate character.

Example

The following example demonstrates how to find all duplicate characters in a string using nested for loop.

public class Example {
   public static void main(String argu[]) {
      String str = "beautiful sea";
      char[] carray = str.toCharArray();
      System.out.println("The string is: " + str);
      System.out.print("Duplicate Characters in above string are: ");
	  // nested for loop to print duplicate characters
      for (int i = 0; i < str.length(); i++) {
         for (int j = i + 1; j < str.length(); j++) {
            if (carray[i] == carray[j]) {
               System.out.print(carray[j] + " ");
               break;
            }
         }
      }
   }
}

The obtained output of the above code is ?

The string is: beautiful sea
Duplicate Characters in above string are: e a u 
Updated on: 2024-08-01T10:46:49+05:30

60K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements