avatarjb stevenard

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2676

Abstract

e contiguous in memory, it permits direct access. Still, it has an impact on inserting or deleting.</p><p id="83de"><b>Appending: </b>Most of the time, appending or adding at the end has a constant cost (not depending on the size of the array). Whenever the array gets full, it needs to reserve more space. It doubles it and moves all existing elements, which has a linear cost (depending on the number of elements).</p> <figure id="b08f"> <div> <div>

            <iframe class="gist-iframe" src="/gist/jbstevenard/682513c76b0e65fb4e727c59d61a007e.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><figure id="0fbc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*cOyk7nd_Vfp5cDfOS1Gk5A.png"><figcaption></figcaption></figure><p id="f849"><b>Inserting: </b>Inserting, in the worst case, is into the first position, which needs to move all elements; even if we don’t reach its capacity, it has a linear cost. If you need to always insert at the beginning of the array, then you should consider another data structure (have a look at the “<a href="https://readmedium.com/linkedlist-ae361fc05839">Linked List</a>”).</p>
    <figure id="0e46">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/jbstevenard/556558c244119a42c112db49d1cdac1e.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><figure id="38aa"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*wgMfK5iMFyF88CdnTh3TKg.png"><figcaption></figcaption></figure><p id="1edd">As inserting, deleting/removing force shifts all elements after the given position, which has a linear cost.</p>
    <figure id="2d6a">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/jbstevenard/6f95079418bd44047d2bb49f6d4844da.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><figure id="61fa"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*XpZDw5mtLyqDGByz2vkkpg.png"><figcaption></figcaption></figure><p id="99ec"><b>Contains: </b>To know if an element is present inside the array, you can use the contains method, which has a linear cost. If you need to check this often, consider another data structure (<a href="https://readmedium.com/sets-b6b7a5dd04f7">Set</a

Options

, <a href="https://readmedium.com/dictionary-b8af7597aa5a">Dictionary</a>, or build your own).</p> <figure id="8755"> <div> <div>

            <iframe class="gist-iframe" src="/gist/jbstevenard/6618f405ad99a85752187e9ff185d20a.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="85dc"><b>Index: </b>If you want to find the position of an element, you can use firstIndex or lastIndex, which have a linear cost.</p>
    <figure id="647e">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/jbstevenard/b0b4db5eb1643bec81e5cf2a29b4717d.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="3739"><b>Count: </b>When using arrays, we often need to know the number of elements or even if it is empty, which has a constant cost.</p>
    <figure id="8d30">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/jbstevenard/63913858a6034fdc823e853619769940.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="0d2a"><b>Time complexity:</b></p><ul><li>Direct access: O(1),</li><li>Append: O(1) (average, worst case o(n)),</li><li>Insert: O(n),</li><li>delete: O(n),</li><li>firstIndex: O(n),</li><li>contains: O(n),</li><li>Iterate: O(n),</li><li>isEmpty: O(1),</li><li>count: O(1).</li></ul><p id="fcd5"><a href="https://readmedium.com/big-o-notation-c221a6ba77cd">&lt;&lt; Big O notation</a> | <a href="https://readmedium.com/learn-data-structures-and-algorithms-with-swift-5-6-d9f36a4027dd">Book</a> | <a href="https://readmedium.com/dictionary-b8af7597aa5a">Dictionaries &gt;&gt;</a></p><div id="e86e" class="link-block">
      <a href="https://medium.com/@jbstevenard/membership">
        <div>
          <div>
            <h2>Join Medium with my referral link - jb stevenard</h2>
            <div><h3>Read every story from jb stevenard (and thousands of other writers on Medium). Your membership fee directly supports jb…</h3></div>
            <div><p>medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*LLOFX6T9gg6rcgut)"></div>
          </div>
        </div>
      </a>
    </div></article></body>

Arrays

An array is a data structure representing a finite sequence of elements that can be accessed efficiently by their position, or index, in the sequence. Elements can be of any type (Int, String, Objects…). Arrays keep the ordering.

Direct Access: While the array is one of the most used data structures, we should use it when we need direct access to any element or iterating following an order.

Sub-array: You can also get a sub-array, Array Slice; it’s fast as a constant cost because they share the same memory with the whole array. If you want a new array, you need to create it, but it will have a linear cost.

Loop: You can easily iterate over an array.

Updating Array: Because elements in arrays are contiguous in memory, it permits direct access. Still, it has an impact on inserting or deleting.

Appending: Most of the time, appending or adding at the end has a constant cost (not depending on the size of the array). Whenever the array gets full, it needs to reserve more space. It doubles it and moves all existing elements, which has a linear cost (depending on the number of elements).

Inserting: Inserting, in the worst case, is into the first position, which needs to move all elements; even if we don’t reach its capacity, it has a linear cost. If you need to always insert at the beginning of the array, then you should consider another data structure (have a look at the “Linked List”).

As inserting, deleting/removing force shifts all elements after the given position, which has a linear cost.

Contains: To know if an element is present inside the array, you can use the contains method, which has a linear cost. If you need to check this often, consider another data structure (Set, Dictionary, or build your own).

Index: If you want to find the position of an element, you can use firstIndex or lastIndex, which have a linear cost.

Count: When using arrays, we often need to know the number of elements or even if it is empty, which has a constant cost.

Time complexity:

  • Direct access: O(1),
  • Append: O(1) (average, worst case o(n)),
  • Insert: O(n),
  • delete: O(n),
  • firstIndex: O(n),
  • contains: O(n),
  • Iterate: O(n),
  • isEmpty: O(1),
  • count: O(1).

<< Big O notation | Book | Dictionaries >>

Algorithms
Data Structures
Coding
Programming
Arrays
Recommended from ReadMedium