Created
May 1, 2013 21:14
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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