Java Program to find duplicate characters in a String?

Java 8Object Oriented ProgrammingProgramming

Following is a Java example which deletes duplicate elements from a given string.

Example

import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;
public class DuplicateSample {
   public static void main(String args[]){
      String str = "malayalam";
      char[] myArray = str.toCharArray();

      for(int i=0; i<myArray.length-1; i++){
         for (int j=i+1; j<myArray.length; j++){
            if(myArray[i] == myArray[j]){
               myArray = ArrayUtils.remove(myArray, j);
            }
         }
      }
      System.out.println("String value after deleting the duplicate values :"+Arrays.toString(myArray));
   }
}

Output

String value after deleting the duplicate values :[m, a, l, y]
raja
Published on 12-Feb-2018 10:51:22
Advertisements