title: Python algorithm to compare two sorted lists and count how many elements are the same
body: <p>I have to design an algorithm that compares two sorted lists of the same length and return the number of common values between them.</p>
  
  <p>So if I have two lists a = [2, 9, 15, 27, 36, 40] and b = [9, 11, 15, 23, 36, 44], the algorithm should return the value of 3 as 9, 15 and 36 are present in both lists.</p>
  
  <p>I know that there might be an easier to do with using sets, but since I'm trying to learn data structures and algorithms I'd prefer to do it the longer(harder way).</p>
  
  <p>My current code uses any array merge algorithm which is non-working at the moment as I'm still confused as to r1 and r2, though i think they would be the right most element in the array, but I don't know how to get that. eg. r1 = 40 (from list a), and r2 = 44 (from list b)?</p>
  
  <pre><code>global a
  a = [2, 9, 15, 27, 36, 40]
  
  global b
  b = [9, 11, 15, 23, 36, 44]
  
  global c
  c = []
  
  def merge (a1, a, r1, a2, b, r2, c, list3):
      i = a
      j = b
      k = c
      r1 = 
      r2 = 
      while i &lt;= r1 and j &lt;= r2:
          if a1[i]&lt;=a2[j]:
              a3[k] = a1[i]
              i += 1
          elif a3[k] &gt;= a2[j]:
              j += 1
              k += 1
      while i &lt;= r1:  
          a3[k] = a1[i]
          i += 1
          k += 1 
      while j &lt;= r2:  
          a3[k] = a2[j]
          j += 1
          k += 1  
  </code></pre>
  
  <p>Thank you for the help and feedback.</p>