I am doing the exercises in the Cracking The Coding Interview book and I am trying to determine if there is a duplicate character in a string. I am using the ArrayList data structure. My method is of return type Boolean, which returns true if there is a duplicate and returns false if there is no duplicate character. I added a third return statement so the program would compile but it always returns false.
import java.util.*;
public class QuestionOneCrackingCode {
public static void main(String[] args) {
String s = "abcdefga";
ArrayList<String> c = new ArrayList<String>();
c.add(s);
System.out.print(check(c));
}
public static boolean check(ArrayList<String> g) {
for (int i = 0; i < g.size(); i++) {
for (int j = i + 1; j < g.size(); j++) {
if (g.get(i) == g.get(j)) {
return true;
} else {
return false;
}
}
}
return false;
}
}