Amazon Interview Question

Given a string, can you remove all duplicate characters in the string.

Interview Answers

Anonymous

Apr 21, 2021

public class RemoveDuplicateString { public static String removeDuplicateFromString(String str){ // Convert String into Char Array // Create TreeSet object // Iterate Char Array and add into TreeSet if(!str.isEmpty()){ char charString[] = str.toCharArray(); Set st = new TreeSet(); for(char ch:charString){ st.add(ch); } return st.toString(); } return ""; } public static void main(String arr[]){ String uniqueString = RemoveDuplicateString.removeDuplicateFromString("aaaabbccd"); System.out.println(uniqueString); } }

4

Anonymous

May 15, 2021

-------------PHP ------------ $str = "Hellooooo"; $split = str_split($str); $char = ''; foreach($split as $sp){ if(strrpos($char, $sp) == 0){ $char .= $sp; } }; echo $char;

2

Anonymous

Mar 30, 2021

x='Hello, worldoo' j="" for i in list(x): if i in j: pass else: j=j+i print(j)

1

Anonymous

Dec 10, 2020

Create result string to append characters if not already in the string.

12