Created
March 19, 2019 20:12
-
-
Save Davidaredding/fdd8f02dd7bdf7546645d66f9315b1dd to your computer and use it in GitHub Desktop.
Character Traversal question in C#
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
| public static char? traverse(string s, bool caseInsensitiveMatching = true) | |
| { | |
| var counts = new Dictionary<char, int>(); | |
| for(int characterIndex=0; characterIndex < s.Length; characterIndex++) | |
| { | |
| var c_upper = s[characterIndex]; | |
| if(!caseInsensitiveMatching) | |
| c_upper = char.ToUpper(s[characterIndex]); | |
| if (counts.ContainsKey(c_upper)) | |
| counts[c_upper] = -1; | |
| else | |
| counts[c_upper] = characterIndex; | |
| } | |
| foreach (var kvp in counts) | |
| if (kvp.Value > -1) | |
| return s[kvp.Value]; | |
| return null; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment