Skip to content

Instantly share code, notes, and snippets.

@kencenerelli
Created May 1, 2013 21:14
Show Gist options
  • Select an option

  • Save kencenerelli/5498434 to your computer and use it in GitHub Desktop.

Select an option

Save kencenerelli/5498434 to your computer and use it in GitHub Desktop.
Interview Question: Compare two strings and return a third string containing only the letters that appear in both.
string firstString = "Hello Ken";
string secondString = "Hello Samuel";
StringBuilder sb = new StringBuilder();
if ((!string.IsNullOrEmpty(firstString)) && (!string.IsNullOrEmpty(secondString)))
{
foreach (char iChar in firstString)
{
if (!sb.ToString().Contains(iChar) && secondString.Contains(iChar))
{
sb.Append(iChar);
}
}
}
Console.Write(sb.ToString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment