This community-built FAQ covers the “Unique Characters in a String” code challenge in Java. You can find that challenge here, or pick any challenge you like from our list.
Top Discussions on the Java challenge Unique Characters in a String
There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in #get-help.
Agree with a comment or answer? Like () to up-vote the contribution!
import java.util.*;
public class UniqueCharacters {
public static void main(String[] args) {
System.out.println(uniqueCharacters(""));
}
public static boolean uniqueCharacters(String strIn) {
if (strIn.length() == 0) {
System.out.println("Error");
return true;
}
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < strIn.length(); i++) {
if (list.contains("" + strIn.charAt(i))) {
return false;
}
list.add("" + strIn.charAt(i));
}
return true;
}
}
This is what I keep trying as my solution, but it doesn’t work for some reason. I keep getting 4/5 tests correct. I suspect it has something to do with this part of the questions: “Note that if the function is called with an empty string (”“), the program should return an error message.” I’m not really sure how to deal with this as you can’t throw an exception of the tester will break, printing a string isn’t enough, and you can’t change the return type from boolean without the tester breaking. Anyone have an answer?
To pass all tests program should not return a boolean along with the error message if the inputted string is blank.
import java.util.*;
public class UniqueCharacters {
public static void main(String[] args) {
System.out.println(uniqueCharacters(""));
}
public static boolean uniqueCharacters(String strIn) {
if (strIn == "")
{
System.out.println("Error");
System.exit(0); // if string is blank, return error and end program
}
char[] strToArray = strIn.toCharArray(); // convert string to char array
Arrays.sort(strToArray); // sort array alphabetically
boolean result = true; // declare boolean result variable and set to true.
// loop through array - compare index i with index i + 1 - bool set to false if they match
for(int i = 0; i < strToArray.length - 1; i++)
{
if (strToArray[i] == strToArray[i+1])
{
result = false;
}
}
return result;
}
}
import java.util.*;
public class UniqueCharacters {
public static void main(String[] args) {
System.out.println(uniqueCharacters("apple"));
}//end main
public static boolean uniqueCharacters(String strIn) {
if(strIn.isEmpty())
{
return false;
}
for(int i=0;i<strIn.length();i++)
{
//check for repeated chart i from i+1 to end of string
if( i!= strIn.length()-1 && strIn.indexOf(strIn.charAt(i),i+1) != -1)
return false;
}
return true;
}//end method uniqueCharacters
}//end class
Nice O(n). I did something similar - yours has a better worst-case complexity but this one has a better best-case:
import java.util.*;
public class UniqueCharacters {
public static void main(String[] args) {
System.out.println(uniqueCharacters("banana"));
}
public static boolean uniqueCharacters(String strIn) {
Set<Character> set = new HashSet<>();
char c = strIn.charAt(0);
set.add(c); //add initial char, no need to check set
for (int i = 1; i < strIn.length(); i++) {
c = strIn.charAt(i);
if(set.contains(c)){return true;}
set.add(c);
}
return false;
}
}