Find Duplicate Characters In String in java


In this core java programming tutorial we will write a program to Find Duplicate Characters In String in java.


Example 1 in java -
inputString is : javamadesoeasy
Output is : aes

Example 2 in java -
inputString is : java
Output is : a

Example 3 in java -
inputString is : ankit
Output is :

Example 4 in java -
inputString is : This is it
Output is : is
Example 5 in java -
inputString is : javaMadeSoEasy.com
Output is : ao

Example/ Program  - Find Duplicate Characters In String in java-
/**
* @author AnkitMittal - Find Duplicate Characters In String in java
*/
public class  FindDuplicateCharacterInString {
  
   public static void main(String[] args) {
      String inputString="javamadesoeasy";
      //String inputString="java"; //Test String
      //String inputString="ankit";  //Test String
      //String inputString="javaMadeSoEasy.com";  //Test String
      //String inputString="This is it";  //Test String
     
       System.out.println("inputString is : "+inputString);
       System.out.print("Output is : ");
       characterCount(inputString);
   }
  
   /**
    * Method calculates count of all characters in inputString.
    */
   public static void characterCount(String inputString){
     
      char[] inputAr=inputString.toCharArray();
      int count=0,arLength;
     
       arLength=inputAr.length;
       for(int x=0; x<arLength; x++){
        char ch=inputAr[x];
        for(int y=x+1; y<arLength; y++){
            if(inputAr[y]==ch){          //if we have find same character again in string
                for(int z=y; z< arLength-1; z++)   //shift characters left.
                inputAr[z]=inputAr[z+1];
                arLength--;   //as characters have been reduce arLength.
                y=x;   //done to tackle case if occurrence of character is more than once in string.
            }
        }
       }
      
       /*
     * Till this point of program, inputAr's first arLength number of elements are unique.
     */
      
       for(int x=0;x<arLength;x++){
          count=0;
       
          for(int y=0; y<inputAr.length; y++){
            if(inputAr[x]==inputString.charAt(y))
            count++;
        }
          if(count >1)
                 System.out.print(inputAr[x]);
       
       }
      
      
   }
  
}
/*
inputString is : javamadesoeasy
Output is : aes
*/


So in this core java programming tutorial we learned how to write a program to Find Duplicate Characters In String in java.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.




RELATED LINKS>








eEdit
Must read for you :