unique in java code example

Example: find unique from a string in java

public class String_FindtheUnique_4 {
/* Write a return  method that can find the unique characters from the String
 Ex:  unique("AAABBBCCCDEF")  ==>  "DEF";  */
    public static void main(String[] args) {

   String result1 = Unique("BBCCDDSSEEDDYUT");
   System.out.println(result1); // YUT

//1.yol 
    String s = "BABABACDR";
    String r = "";
    for(String each : s.split(""))
 r += ( (Collections.frequency(Arrays.asList(s.split("")), each)) ==1 ) ? each : "";
     System.out.println(r); // CDR
    }

//2.yol method yolu
  public static String Unique(String str) {
        String result1 ="";
        for(String each : str.split(""))
result1 += ( (Collections.frequency(Arrays.asList(str.split("")), each)) ==1 ) ? each : "";
        return result1;
    }

}

Tags:

Java Example