avatarjb stevenard

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

2149

Abstract

</div> </div> </figure></iframe></div></div></figure><p id="62be">Because of the <b>non-unicity of hashing and the modulo,</b> that will lead us to <b>index collisions</b>. It means that<b> two different keys will end up in the same bucket</b>. Once we have identified the bucket, we need to check into that bucket linearly until finding the right key.</p> <figure id="8a51"> <div> <div>
            <iframe class="gist-iframe" src="/gist/jbstevenard/2959f8ee4e9385d4c6e7900295725301.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><figure id="769d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*-GMjCmlNBDatZFd06kibag.png"><figcaption></figcaption></figure><p id="7cb9">While a dictionary is one of the most used data structures, we should use it when we need <b>key access to its value</b>. For example, we want to count the number of occurrences of a character within a string. On the contrary, if you want only to check an element’s presence, please look at the <a href="https://readmedium.com/sets-b6b7a5dd04f7">Set</a> data structure.</p>
    <figure id="f7e9">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/jbstevenard/eb38b31d12e5e6375d6ebeaf2e63b12d.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="f18f">To set a new key/value pair.</p>
    <figure id="5a1a">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/jbstevenard/466dbe5da52f481ab1576c70921376ac.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="47b8">You can easily iterate over a dictionary, but it does not guarantee the order.</p>
    <figure id="4e5f">
        <div>
       

Options

<div>
            <iframe class="gist-iframe" src="/gist/jbstevenard/ef62ceebf30cb83fcb60783fe03de82e.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="6ac1">You can get all keys, or all values, then sort them; you will be able to get always the same order but might not get the same as the elements adding order.</p>
    <figure id="01f6">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/jbstevenard/710e7a393bf99e662b1cb7ec3b1a0b0b.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="5382">The number of buckets is called capacity; a well-sized dictionary leads to a very good average performance O(1), while a single bucket will lead to a simple list performance O(n).</p><p id="8428"><b>Time complexity:</b></p><ul><li>key access: O(1),</li><li>set: O(1),</li><li>delete: O(1),</li><li>contains: O(1),</li><li>Iterate: O(n),</li><li>isEmpty: O(1),</li><li>count: O(1).</li></ul><p id="e471"><a href="https://readmedium.com/array-ad561d6f4a58">&lt;&lt; Arrays</a> | <a href="https://readmedium.com/learn-data-structures-and-algorithms-with-swift-5-6-d9f36a4027dd">Book</a> |<a href="https://readmedium.com/strings-b85520c7b800">Strings &gt;&gt;</a></p><div id="b436" 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>

Dictionaries

A dictionary (map or hashtable) is a data structure representing a collection of pairs of key/values. You can access all values by their key. All values can be of any type (Int, String, Objects, …). Still, the keys have to be hashable (all primitive types implement this protocol). Dictionaries don’t guarantee the ordering.

You must wonder why keys have to be hashable. Let’s say that hashable transform any object into an integer that will use to define where the value is stored. The better the hashing is better the int will be unique.

Each dictionary is composed of an array of buckets (each bucket will be a linked list, to learn more about it, please refer to its article). Let’s say we have only three buckets with our previous example; we will use the bucket at index 1. Because this is a limited number of arrays, we need to use a modulo of the key’s hash.

Because of the non-unicity of hashing and the modulo, that will lead us to index collisions. It means that two different keys will end up in the same bucket. Once we have identified the bucket, we need to check into that bucket linearly until finding the right key.

While a dictionary is one of the most used data structures, we should use it when we need key access to its value. For example, we want to count the number of occurrences of a character within a string. On the contrary, if you want only to check an element’s presence, please look at the Set data structure.

To set a new key/value pair.

You can easily iterate over a dictionary, but it does not guarantee the order.

You can get all keys, or all values, then sort them; you will be able to get always the same order but might not get the same as the elements adding order.

The number of buckets is called capacity; a well-sized dictionary leads to a very good average performance O(1), while a single bucket will lead to a simple list performance O(n).

Time complexity:

  • key access: O(1),
  • set: O(1),
  • delete: O(1),
  • contains: O(1),
  • Iterate: O(n),
  • isEmpty: O(1),
  • count: O(1).

<< Arrays | Book |Strings >>

Data Structures
Algorithms
Coding
Programming
Dictionary
Recommended from ReadMedium