Pages

Friday, December 19, 2014

Java program to find the duplicate element in the given string sentences and its count.

import java.util.HashSet;
public class DuplicateElement 
{
 public static void main(String a[])
{
    String Str="welcome to program";
        DuplicateElement dcs = new DuplicateElement();
        for (String retval: Str.split("\\s", 0))
        {
        dcs.duplicate(retval);
         }
  }
 public static void duplicate(String str)
    {
    char[] chrs = str.toCharArray();
    HashSet s=new HashSet();
    for(int i=0;i
    {
    if(!s.add(chrs[i]))
    {
   System.out.println("The duplicate element contain in the word : "+str);
    System.out.println("and its count is : "+str.length());
    }
    }
   
    }
}

No comments:

Post a Comment