Skip to content

Instantly share code, notes, and snippets.

@Davidaredding
Created March 19, 2019 20:12
Show Gist options
  • Save Davidaredding/fdd8f02dd7bdf7546645d66f9315b1dd to your computer and use it in GitHub Desktop.
Save Davidaredding/fdd8f02dd7bdf7546645d66f9315b1dd to your computer and use it in GitHub Desktop.
Character Traversal question in C#
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