title: Array Contains Too Slow Swift
body: <p>I have been porting over an algorithm I've been using in Java (Android) to Swift (iOS), and have run into some issues with speed on the Swift version.</p>
  
  <p>The basic idea is there are objects with depths (comment tree), and I can hide and show replies from the dataset by matching against a list of hidden objects. Below is a visualization</p>
  
  <pre><code>Top
  - Reply 1
  - - Reply 2
  - - Reply 3
  - Reply 4
  </code></pre>
  
  <p>and after hiding from the dataset</p>
  
  <pre><code>Top
  - Reply 1
  - Reply 4
  </code></pre>
  
  <p>The relevant methods I've converted from Java are as follows</p>
  
  <pre><code> //Gets the "real" position of the index provided in the "position" variable. The comments array contains all the used data, and the hidden array is an array of strings that represent items in the dataset that should be skipped over.
  
      func getRealPosition(position: Int)-&gt; Int{
  
          let hElements = getHiddenCountUpTo(location: position)
          var diff = 0
          var i = 0
          while i &lt; hElements {
              diff += 1
              if(comments.count &gt; position + diff &amp;&amp; hidden.contains(comments[(position + diff)].getId())){
                  i -= 1
              }
              i += 1
          }
          return position + diff
      }
  
      func getHiddenCountUpTo(location: Int) -&gt; Int{
          var count = 0
          var i = 0
          repeat {
              if (comments.count &gt; i &amp;&amp; hidden.contains(comments[i].getId())) {
                  count += 1
              }
              i += 1
          } while(i &lt;= location &amp;&amp; i &lt; comments.count)
          return count
      }
  </code></pre>
  
  <p>This is used with a UITableViewController to display comments as a tree. </p>
  
  <p>In Java, using array.contains was quick enough to not cause any lag, but the Swift version calls the getRealPosition function many times when calling <code>heightForRowAt</code> and when populating the cell, leading to increasing lag as more comment ids are added to the "hidden" array. </p>
  
  <p>Is there any way I can improve on the speed of the array "contains" lookups (possibly with a different type of collection)? I did profiling on the application and "contains" was the method that took up the most time.</p>
  
  <p>Thank you</p>