Samsung Interview Question
Senior Software Development EngineersCountry: United States
Interview Type: In-Person
class Program
{
static void Main(string[] args)
{
char[] ch = Console.ReadLine().ToCharArray();
for(int i=0;i<ch.Length;i++)
{
if(ch[i]=='~')
{
continue;
}
for(int j=i+1;j<ch.Length;j++)
{
if(ch[i]==ch[j])
{
ch[j] = '~'; // Ssave any special char
}
}
}
for(int j=0;j<ch.Length;j++)
{
if(ch[j]=='~')
{
continue;
}
Console.Write(ch[j]);
}
}
}
Can be done in 2 ways:
1) Brute Force using 2 loops.
O(N^2) Runtime.
2) Using HashMap to remember the char already present.
O(N) Runtime with Constant Space
- R@M3$H.N September 24, 2014