string find unique in java code example
Example 1: how to find unique characters in a string in java
String str = "abbcdddefffggghjilll";
String [] arr = str.split("");
Map<String, Integer> mapStr = new LinkedHashMap<>();
for (int i=0 ; i < arr.length ; i++){
if (!mapStr.containsKey(arr[i])){
mapStr.put(arr[i],1);
} else{
mapStr.put(arr[i],mapStr.get(arr[i])+1);
}
}
for (Map.Entry<String,Integer> map : mapStr.entrySet()) {
if(map.getValue()==1) {
System.out.print(map.getKey());
}
}
Example 2: find unique characters in java
String str = "abbcdddefffggghjilll";
String [] arr = str.split("");
Map<String, Integer> mapStr = new LinkedHashMap<>();
for (int i=0 ; i < arr.length ; i++){
if (!mapStr.containsKey(arr[i])){
mapStr.put(arr[i],1);
} else{
mapStr.put(arr[i],mapStr.get(arr[i])+1);
}
}
for (Map.Entry<String,Integer> map : mapStr.entrySet()) {
if(map.getValue()==1) {
System.out.print(map.getKey());
}
}
Example 3: find unique from a string in java
public class String_FindtheUnique_4 {
public static void main(String[] args) {
String result1 = Unique("BBCCDDSSEEDDYUT");
System.out.println(result1);
String s = "BABABACDR";
String r = "";
for(String each : s.split(""))
r += ( (Collections.frequency(Arrays.asList(s.split("")), each)) ==1 ) ? each : "";
System.out.println(r);
}
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;
}
}