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

3
128
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share


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

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

3 COMMENTS

  1. String str="HAPPYTESTING";
    String str2="";
    String temp1="";
    String temp2="";
    int count=0;
    for(int i=0;i<str.length();i++)
    {
    for(int j=0;j<str.length();j++)
    { temp1=""+str.charAt(j);
    temp2=""+str.charAt(i);
    if(temp1.equalsIgnoreCase(temp2))
    count++;
    }
    if(count==1){
    count=0;
    System.out.println("Non Repeatative Character is:"+str.charAt(i));
    }
    else{
    if(!str2.contains(""+str.charAt(i)))
    {
    str2=str2+str.charAt(i);
    System.out.println("Repeatative Character is:"+str.charAt(i));System.out.print("");
    }
    count=0;
    }

    }
    }

LEAVE A REPLY

Please enter your comment!
Please enter your name here