Coding Challenge Day-2: Problem 2: Determine if a String has all unique characters

Hello! So I’m doing a 30 day coding challenge where I solve around 3-5 questions per day and thought of posting them here on my blog so that you guys can join the challenge too!

Welcome to Coding challenge Day 2: Problem two! Be sure to post your answers, queries etc in the comments!!

Problem: Determine if a String has all unique characters

Sample input: Root
Output: All characters in Root are not unique

Sample input: Flower
Output: All characters in Flower are unique

Sample input: Tech Art
Output: All characters in Tech Art are unique


  • Solved this assuming that the character set is ASCII
  • So since there are only 128 unique elements in ASCII so if the length of the string is greater than 128 then of course there will be some duplicate characters
  • create an boolean array where true indicates that that particular index is occupied by a corresponding character (ascii and index number are same) from string
  • then we start taking out each character from the string
  • check if the character at that index number in array is true or false
  • if that index is already occupied then we return false
  • if that index is not occupied then we write that character at corresponding index number in array
  • if the function returns true then the string has all unique characters else all characters are not unique
package array;
//find if a given string has all unique elements
public class Program002unique_string {
	public static boolean unique_check (String str){
		if(str.length()>128) //128 is number of unique characters in ASCII
			return false;
		boolean [] array= new boolean[256];
		for(int i=0; i<str.length(); i++){
			int val= str.charAt(i);
				return false;
		return true;
	public static void main (String [] args){
		String str="Tech Art";
		boolean r= unique_check(str);
			System.out.printf("All characters in %s are not unique", str);
			System.out.printf("All characters in %s are unique", str);


Happy Learning!!

Leave a Reply


Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close