[c,a,a,b,b,d], [c,a,b,b,d,d,a] or [a,a,b,b,c,d,d] should return false while [a,a,c,d,b,b] or [a,a,c,b,b,d] should return true as the duplicates are not consecutive.
public boolean findNSeqDup(String[] arr) {
if(arr.length < 4){
return false;
}
boolean isPrevDup = false;
String prevString = "";
for (int i = 0; i < arr.length; i++){
if(arr[i].equals(prevString) && isPrevDup==true){
System.out.println("true");
return true;
}
else if(arr[i].equals(prevString) && isPrevDup==false){
isPrevDup = true;
prevString = "";
}
else if(!prevString.equals("") && !arr[i].equals(prevString)){
prevString = "";
}
// for the first condition if the previous string is empty
else {
prevString = "";
}
prevString = arr[i];
}
System.out.println("false");
return false;
}
The above code returns true to all conditions where it should return false ([c,a,a,b,b,d], [c,a,b,b,d,d,a] or [a,a,b,b,c,d,d]). What am I doing wrong? Will this be easier if I used a stack or a similar data structure?