I have retackled this problem using the help I received from here: Remove duplications from a Java String but this time using a LinkedHashSet
since previosuly I was using a HashSet
but the answer was out of order.
From this implementation my run time should be \$O(n)\$ correct? Does anyone see any room where I can improve my code or some mistakes?
public static void main(String[] args){
String test = "Banana";
LinkedHashSet knownChars = new LinkedHashSet();
StringBuilder noDups = new StringBuilder();
for(Character c : test.toCharArray()){
if(!knownChars.contains(c)){
knownChars.add(c);
noDups.append(c);
}
}
System.out.println("No duplicate string is: " + noDups);
}