Unique characters in line:
This is the main topic of a Java interview where the interviewer wants to test knowledge of HashSet or indexOf (in the case of Java 7). Let's deal with the issue. Let's say the interview tells you
Check the uniqueness of the string: HashSet provides uniqueness. In other words, each object in a HashSet is represented only once. Therefore, we will use a HashSet.
import java.util.HashSet; import java.util.Set; public class Abc { public static void main(String[] args) { String a = "Gini"; String aa = a.toLowerCase(); if( isUnique(aa) ) { System.out.println("All characters are unique"); }else { System.out.println("All characters are not unique"); } } public static boolean isUnique(String a ) { Set< Character> set = new HashSet<>(); char[] charArray =a.toCharArray(); for(Character ch :charArray) { if(!set.add(ch)) { return false; }
The output in the case of GINI will be: All characters are not unique
Now the number of unique characters. Here we will also use HashSet because of its uniqueness.
import java.util.HashSet; public class practice11 { public static void main(String[] args) { String a = "Gini"; String aa = a.toLowerCase(); System.out.println(countUniqueCharacters(aa)); } public static int countUniqueCharacters(String a) { char[] charArray = a.toCharArray(); HashSet<Character> set = new HashSet<Character>(); for(int i = 0 ; i< charArray.length ; i++) { set.add(charArray[i]); }
The exit for Gini will be 3 (Gin will be considered).
The indexOf: indexOf () method returns the index of the first occurrence of a character, and then we compare it with -1.
public class Abc { public static void main(String[] args) { String a = "Gini"; String aa = a.toLowerCase(); String t = " "; for (int i = 0; i < aa.length(); i++) { int pp = aa.charAt(i) ; if(t.indexOf(aa.charAt(i)) == -1 ) { t = t + aa.charAt(i); }
In Java 8, this is extremely simple. We just need to use chars (). Different (). Count (). But the type of return will be long.
class Abc{ public static void main(String[] args) { String a = "Gini"; String aa = a.toLowerCase(); System.out.println( countUnique(aa)); } private static long countUnique(String aa) {
Another classic interview question: find the first non-repeating character in the string OR the first unique character in the string. This problem can be solved using HashMap knowledge.
class Abc{ public static void main(String[] args) { String a = "GinaRani" ;
We will do this with a list comprehension in Python. We do not use the set () operator. In Python:
string = 'GiniGinaProtijayi' unique = [] [ unique.append(ch) for ch in string if ch not in unique ] lengthofUniqueCharacters = len(unique) print("length of Unique Characters => " ,lengthofUniqueCharacters) print("as a list => ",unique) print("as a string => " , ''.join(unique))
Just to print single characters in Java 8:
public class Test5 { public static void main(String[] args) { String a = "GinaGini"; String aa = a.chars().distinct() .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) .toString(); System.out.println(aa);