Write a java program to find unique character in string


Program
import java.util.*; public class nonuniquetwo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String sentence = sc.nextLine(); String[] words = sentence.split(" "); for(String word : words) { System.out.print(getUnique(word) + " "); } sc.close(); } public static String getUnique(String str) { StringBuffer sb = new StringBuffer(); for(int i = 0; i<str.length(); i++) { if(countFrequency(str.charAt(i), str) == 1) { sb.append(str.charAt(i)); } } String result = sb.toString(); return result; } public static int countFrequency(char c, String str) { int count = 0 ; for(int i = 0; i<str.length(); i++) { if(str.charAt(i) == c) { count++; } } return count; } }
Input
Output