How to find unique characters from given string in java. Java code to find all the unique characters from given string. How to write complete code in java to find unique characters in given string.
what are Unique characters ?
A character which is present only once in a given string is called as “unique character”. So if a particular character is present exactly once inside the given string then it is called as “UNIQUE CHARACTER”.
Examples for Unique Characters
Example1: Let the given string be : “Vrashikesh patil”
Unique characters for given string are : v, r, k, e, p, t, l
So in above examle we can see the given string is “vrashikeh patil” and the characters which are unique here are v, r, k, e, p, t, l
Example2: Let the given string be : “Dattatrey patil”
Unique characters for given string are: D, r, e, y, p, i, l
So in above examle we can see the given string is “Dattatrey patil” and the characters which are unique here are D, r, e, y, p, i, l
Algorithm to find Unique Elements in string
- Initially take the input from user in form of string
- convert the given string into String buffer.
- The above step is needed because addition and deletion of elements is not possible in string.
- If a character is not repeated then it’s unique, so set one counter to ZERO which is further used to evaluvate the output.
- Using 2 for loops cascaded, the outer for loop is for holding one particular character and inner for loop is used to iterate over other elements of the given string.
- So when outer loop executes for 1 time, the inner loop will be getting executed for n number of times (n indicates the length of given string).
- Inside the inner for loop just check if the elemnts are matching, if true then increase the counter by 1.
- so at the end of inner for loop just check wether counter value is >1, if true then it’s a duplicate element, otherwise it’s a unique element so print it.
- So when the outer for loop gets executed completely all the unique characters will get printed.
JAVA CODE for printing Unique Characters
Using String Buffer
package arrayPrograms; import java.util.*; class StringMethod { public String getUnique(String str) { // code to print Unique elements // Coded by VRASHIKESH PATIL String string4=""; StringBuffer sb = new StringBuffer(str); for(int i = 0; i< sb.length();i++){ int count1 = 0; for(int j = i+1;j<sb.length();j++){ if(sb.charAt(i)==sb.charAt(j)){ sb.deleteCharAt(j); j--; count1++; } } if(count1 >= 1){ sb.deleteCharAt(i); i--; } } for(int i=0;i<sb.length();i++) { string4=string4+sb.charAt(i); } return(string4); } public static void main(String[] args) { String string1="Doselect is a Great Platform"; StringMethod s1= new StringMethod(); System.out.println(s1.getUnique(string1)); } }
Watch Explanation for above code here
Using String
package arrayPrograms; import java.util.*; class StringMethod { public static void main(String[] args) { String string1="Doselect is a Great Platform"; char string[] = string1.toCharArray(); int count; String string4=""; StringBuffer sb = new StringBuffer(string1); for(int i = 0; i< sb.length();i++){ int count1 = 0; for(int j = i+1;j<sb.length();j++){ if(sb.charAt(i)==sb.charAt(j)){ sb.deleteCharAt(j); j--; count1++; } } if(count1 >= 1){ sb.deleteCharAt(i); i--; } } for(int i=0;i<sb.length();i++) { string4=string4+sb.charAt(i); } System.out.println(string4); } }
Watch Explanation for above here
Similar off campus coding questions
Java code to print duplicate characters from string
TCS Off Campus coding question solved
Pingback: Important Difference between array list and linked list - Is It Actually