Header Ads

Find Repeated Character From An Array of String In Java/Selenium/OATS


You have a array of strings e.g ["abcd","aabcd",xyz", "ccbc"] find the strings in
which characters are repeated.Output would be ["aabcd","ccbc"].

Logic:: Need to check if the current position and the next position are having the same character or not.If they have same character they are repetitive characters.


//You have a array of strings e.g ["abcd","aabcd",xyz", "ccbc"] find the strings in 
//which characters are repeated.Output would be ["aabcd","ccbc"].
//Author:Animesh



public class RepeatString {
 
 static String[] names={"abcd","aabcd","xyz", "cdbbc"};
 
 public static void main(String[] args) throws InterruptedException {
  for(int i=0;i<names.length;i++)
  {
   if(repeatCalulator(names[i]))
   {
    System.out.println(names[i]);
   }
  }
  
  
 }
 
 public static boolean repeatCalulator(String name)
 {
  boolean flag=false;
  int sizeoftheString=name.length();
  for(int j=1;j<sizeoftheString;j++)
  {
  if(name.charAt(j-1)==name.charAt(j))
  {
   
   flag=true;
   break;
   
  }
    
  }
  return flag;
  
 }

}




Image source:www.javaclass.info
Powered by Blogger.