/// <summary> /// Counts the amount of certain character occasions in a string. /// <para>Has a "skip list" which prevents checking a character more than once. /// If a character is found more than once => skip current character.</para> /// </summary> /// <param name="stringToSearchIn"></param> /// <returns></returns> public static char CountEachCharacterSearch(string stringToSearchIn) { char foundCharacter = default; List <char> checkedCharacters = new List <char>(); int stringLength = stringToSearchIn.Length; for (int i = 0; i < stringLength; i++) { char currentCharacter = stringToSearchIn[i]; // Don't check the same character twice if (checkedCharacters.Contains(currentCharacter)) { continue; } // If the character count is higher than 1 - add it to "skip list" so that you don't check it again if (StringCheck.CharacterRepeats(stringToSearchIn, currentCharacter)) { checkedCharacters.Add(currentCharacter); continue; } else // If the count is equal to 1 (can't be lower, because we are checking it from the existing string) => character found { foundCharacter = currentCharacter; break; } } return(foundCharacter); }