Oracle Interview Question
Given a string, print unique elements from the string(Order doesn't matter)
Eg: Input: "abbbfjhuuyyd" output: ('a', 'b', 'f', 'j', 'h', 'u', 'y', 'd')
Interview Answers
this question was like a warm up kind of one and I solved this using HashSet.
public static void main(String[] args) {
String s = "abbbfjhuuyyd";
int i = 0;
int j = 0;
while (j <= s.length()) {
if (((j < s.length()) && (s.charAt(i) != s.charAt(j))) || (j == s.length())) {
System.out.print(s.charAt(i) + " ");
i = j;
}
j++;
}
}
By using lodash.js, you can simply pass the string to the existing function " _.uniq" which returns only the unique elements