title: Time Complexity of Python `rfind()`
body: <p>I'm wondering if using <code>index</code> or <code>rindex</code> would increase the time complexity to O(n^2) in a loop such as:</p>
  <pre class="lang-py prettyprint-override"><code>for c in s:
      if s.index(c) == s.rindex(c):
          return c
  </code></pre>
  <p>I'm not proficient in C. However, I researched the source code for <code>rindex.</code> Which can be found <a href="https://github.com/python/cpython/blob/29f1b0bb1ff73dcc28f0ca7e11794141b6de58c9/Objects/unicodeobject.c#L12235" rel="nofollow noreferrer">here</a>. I did notice that they are using some sort of nested while loop, which technically would make the time complexity O(n^2), but that is not always the case.</p>
  <p>Edit: Also, the code above seems to be more efficient than something like this:</p>
  <pre class="lang-py prettyprint-override"><code> h1 = dict()
      
   for index, element in enumerate(s):
       h1[element] = h1.get(element, 0) + 1
      
   for key, value in h1.items():
       if value == 1:
           return key
  </code></pre>
  <p>That said, please enlighten me in understanding if the code above is O(n) or O(n^2) and why it is more efficient.</p>
  <p>Tested with a string of length 100k.</p>
  <p>I believe the code is O(n^2) due to using an O(n) inside a for a loop. But please correct me if I'm wrong.</p>