Java Challenge - Unique Characters in a String

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 (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 (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 (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

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?

I pass same 4/5. Not sure where the problem is.

import java.util.*;

public class UniqueCharacters {
  public static void main(String[] args) {
    System.out.println(uniqueCharacters("apple"));
  }

  public static boolean uniqueCharacters(String strIn) {
        if(strIn == null || strIn.isEmpty()){
            System.out.println("Bad string");
            return false;
        }
        for (int i = 0; i < strIn.length() - 1; i++) {
            for (int j = i + 1; j < strIn.length(); j++) {
                if (strIn.charAt(i) == strIn.charAt(j)) {
                    return false;
                }
            }
        }
        return true;
    }

}

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;
  }
}
4 Likes

Thank you so much, this works

2 Likes
import java.util.*;

public class UniqueCharacters {
  public static void main(String[] args) {
    System.out.println(uniqueCharacters("apple"));
  }

  public static boolean uniqueCharacters(String strIn) {
    if (strIn.isEmpty()){
      return false;
    }
    for (int i = 0; i < strIn.length() - 1; i++) {
      for (int j = i + 1; j < strIn.length(); j++) {
        if (strIn.charAt(i) == strIn.charAt(j)) {
          return false;
        }
      }
    }
    return true;
  }

}

This is how I solved this challenge:

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

My simple java solution!!

import java.util.*;

public class UniqueCharacters {
  public static void main(String[] args) {
    System.out.println(uniqueCharacters("apple"));
  }

  public static boolean uniqueCharacters(String strIn) {

    char[] str = strIn.toCharArray();

    if(str.length==0)
    {
      return false;
    }
    else
    {
      for(int i = 0; i<str.length; i++)
      {
        for(int j = i+1; j<str.length; j++)
        {
          if(str[i]==str[j])
          {
            return false;
          }
        }
      }
    }
    return true;
    
  }

}

import java.util.*;

public class UniqueCharacters {
public static void main(String args) {
System.out.println(uniqueCharacters(“apple”));
}

public static boolean uniqueCharacters(String strIn) {

    if (strIn.isEmpty()){
        return  false ;
    }

    Set<Character> wordSet = new HashSet<>();
    for (char c :strIn.toCharArray() ){
        wordSet.add(c);
    }

    return  strIn.toCharArray().length == wordSet.size();

}

}

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;
  }
}