Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,166 questions

40,722 answers

573 users

How to check if a string contains only unique characters in Java

1 Answer

0 votes
import java.util.*; 

public class MyClass {
    static boolean contains_unique_chars(String s) { 
        char[] arr = s.toCharArray(); 
  
        Arrays.sort(arr); 
  
        for (int i = 0; i < arr.length - 1; i++) { 
            if (arr[i] == arr[i + 1]) 
                return false; 
        } 
        return true; 
    } 
    public static void main(String args[]) {
        String s = "abcde"; 
    
        if (contains_unique_chars(s)) { 
            System.out.println("yes");
        } 
        else { 
            System.out.println("no");
        } 
    }
}

 





answered Dec 29, 2019 by avibootz

Related questions

1 answer 83 views
2 answers 149 views
2 answers 101 views
2 answers 157 views
...