JAVA PROGRAM TO PRINT ALL THE DUPLICATE CHARACTERS IN A STRING

 

 

A  String  s  is  given ,  identify  all  the  characters   that  appear  more  than  once  and  print  those  characters  along  with  it’s  count  of  each.

 

Examples:

 

Input : s=”happymindhappylife”;

 

Output :   

h 2     a 2      p  4       y  2       i   2

 

Input :   s=”programming”;

 

Output :

 r   2       g 2     m 2  

 

 

 

 

 

 

import   java.util.*;

 

public   class    Countdupchar{

public   static  void  main (String  args[] ) {

String  str=“happymindhappylife”;

char   carray[]=str.toCharArray();

int  length=carray.length;

 

for(int  i=0;i<length;i++) {

       int  count=1;

       for(int  j=i+1;j<length; j++){

       if(carray[i]==carray[j]){

            count++;

            //To  Stop  Printing  already  visited  character

            carray[j]=’0′;

        }

        }

  //Print  the  duplicate  characters  present  in the  String

 

   if(count!=1  &&  carray[i]!=’0′ ) {

         System.out.println(carray[i] + ”   ” +count+ ”  “);

  }

     }

 

  }

    }

 

 

 

 

 

 

 

OUTPUT

 

h 2    a  2       p  4       i   2

 

Leave a Comment

Your email address will not be published. Required fields are marked *