Interview Questions

232. Given a string you have to remove all the duplicate elements from the string ... .....

Microsoft Interview Questions and Answers


(Continued from previous question...)

232. Given a string you have to remove all the duplicate elements from the string ... .....

Question:
Given a string you have to remove all the duplicate elements from the string and place them at the end of the string.
For Example for the string "abbccddacde"
the output is "abcdebcdacd".
see that abcde are the unique elements and the repeated elements are in the order in which appear in the original string.
Give an O(n) solution for this.


maybe an answer:


{
static void Main(string[] args)
{
string input = "abbccddacde";
Console.WriteLine(input);
Console.WriteLine(StringRemDuplicates(input));
Console.ReadLine();
}
static string StringRemDuplicates(string input)
{
Hashtable foundChars = new Hashtable();

Queue uniqueChars = new Queue();
Queue duplicateChars = new Queue();
for (int i = 0; i < input.Length; i++)
{
if (foundChars[input[i]] == null)
{
foundChars.Add(input[i],"");
uniqueChars.Enqueue(input[i]);
}
else
{
duplicateChars.Enqueue(input[i]);
}
}

string output = "";
while (uniqueChars.Count > 0)
output += uniqueChars.Dequeue().ToString();
while (duplicateChars.Count > 0)
output += duplicateChars.Dequeue().ToString();
return output;
}

(Continued on next question...)

Other Interview Questions