static SubString FindMaxSubStrings(int len, String searchString) { var subString = new SubString(); Dictionary <string, int> tempMap = new Dictionary <string, int>(); for (var offset = 0; offset < (searchString.Length - len); offset++) { var sub = searchString.Substring(offset, len); if (!tempMap.ContainsKey(sub)) { tempMap.Add(sub, 1); } else { tempMap[sub]++; } } var mostOccurences = tempMap.Values.Max(); // Allows for faster access in the loop subString.SetOccurrences(mostOccurences); foreach (var entry in tempMap) { if (entry.Value == mostOccurences) { subString.SetValue(entry.Key); return(subString); } } return(subString); //This should never be reached in theory }
static void Main(string[] args) { SubString myString = FindMaxSubStrings(3, "zfabcde224lkfabc51+crsdtab="); myString.PrintInfo(); }