Note that there are some explanatory texts on larger screens.

plurals
  1. POCharacter Repetition
    text
    copied!<p>I am writing a simple program to calculate the repetition of characters in a string sequence. The program I have for now is as below, but I am looking to see if it can be optimized any further. I believe the program right now is O(n) worst case time and I would like to see if there is something that can give me O(log n) running time.</p> <pre><code>using System; using System.Collections.Generic; namespace Algos { class CharacterRepitition { private char[] checkStringArray; private bool[] discovered; public CharacterRepitition(string toCheck) { checkStringArray= toCheck.ToCharArray(); discovered= new bool[checkStringArray.Length]; for (int i = 0; i &lt; checkStringArray.Length; i++) { discovered[i] = false; } } public void CheckRepetitions() { int charIndex=0; Dictionary&lt;char, int&gt; repetitions = new Dictionary&lt;char, int&gt;(); while (charIndex &lt; checkStringArray.Length) { int count = 0; if(discovered[charIndex].Equals(false)) { count = RunThroughTheString(charIndex, checkStringArray); if (count &gt; 0) { repetitions.Add(checkStringArray[charIndex], count+1); } } charIndex++; } if (repetitions.Count == 0) { Console.WriteLine("\nNo characters repeated."); } else { foreach (KeyValuePair&lt;char, int&gt; result in repetitions) { Console.WriteLine("\n'"+ result.Key + "' is present: " + result.Value + " times."); } } } private int RunThroughTheString(int currentCharIndex, char[] checkStringArray) { int counter = 0; for (int i = 0; i &lt; checkStringArray.Length; i++) { if (checkStringArray[currentCharIndex].Equals(checkStringArray[i]) &amp;&amp; i !=currentCharIndex) { counter++; discovered[i] = true; } } return counter; } } } </code></pre> <p>I know i can achieve this with LINQ as well. But that is not something I am looking for. Appreciate your help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload