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

Anonymous

Jul 17, 2018

this question was like a warm up kind of one and I solved this using HashSet.

Anonymous

Aug 2, 2019

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++; } }

Anonymous

Feb 10, 2020

By using lodash.js, you can simply pass the string to the existing function " _.uniq" which returns only the unique elements